leetcode 61. copy-list-with-random-pointer 旋转链表 python3

时间:2020-7-10

题目地址:https://leetcode-cn.com/problems/copy-list-with-random-pointer/

题目难度:Medium

题目描述:

给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数。

示例 1:

输入: 1->2->3->4->5->NULL, k = 2
输出: 4->5->1->2->3->NULL
解释:
向右旋转 1 步: 5->1->2->3->4->NULL
向右旋转 2 步: 4->5->1->2->3->NULL
示例 2:

输入: 0->1->2->NULL, k = 4
输出: 2->0->1->NULL
解释:
向右旋转 1 步: 2->0->1->NULL
向右旋转 2 步: 1->2->0->NULL
向右旋转 3 步: 0->1->2->NULL
向右旋转 4 步: 2->0->1->NULL


思路1:找有效的需要旋转的k 逐个取最后一个结点放到头结点去

代码段1:通过

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def rotateRight(self, head: ListNode, k: int) -> ListNode:
        if head == None:
            return None
        if head.next == None:
            return head
        prev_node = ListNode(0)
        prev_node.next, new_link = head, head
        length = 0
        while head:
            length += 1
            head = head.next
        time = k %length
        for _ in range(time):
            new_link = self.change_fl(new_link)
        return new_link

    def change_fl(self, head):
        temp = head
        while temp.next.next:
            temp = temp.next
        temp_head = temp.next
        temp.next = None
        temp_head.next = head
        #print(temp_head)
        return temp_head

总结:

  1. 终于在中等题里找到了点自信,我的马鸭

思路2:说是循环旋转,但其实本质上是将尾部向前数第K个元素作为头,原来的头接到原来的尾上

代码段2:通过

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def rotateRight(self, head: ListNode, k: int) -> ListNode:
        if head == None:
            return None
        if head.next == None:
            return head
        prev_node = ListNode(0)
        prev_node.next, new_link = head, head
        length = 0
        while prev_node.next:
            length += 1
            prev_node = prev_node.next
        time = k % length
        fast = head
        go_fast = length - time - 1
        while go_fast:
            fast = fast.next
            go_fast -= 1
        
        new_head = fast.next
        fast.next = None
        fast = new_head
        while fast.next:
            fast = fast.next
        fast.next = head
        return new_head

总结:

  1. 时间节省了不少

思路3:连成环

代码段3:通过

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def rotateRight(self, head: ListNode, k: int) -> ListNode:
        if not head:
            return head
        pre = head
        depth = 0
        while head:
            depth += 1
            if not head.next: break
            head = head.next
        head.next = pre
        

        head.next = pre
        k  = k % depth
        move = depth - k

        while move:
            pre = pre.next
            head = head.next
            move -= 1
        head.next = None
        return pre

总结:

  1. 这个环的思路挺妙的,时间也没好到那里
  2. 我终于明白我的短板是:空间想象,设计多个指针的,递归的,我指定懵。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值