LeetCode Rotate List

LeetCode解题之Rotate List

原题

将一个链表中的元素向右旋转k个位置。

注意点:

  • k可能非常大
  • 最好不要申请额外空间

例子:

输入: list = 1->2->3->4->5->NULL, k = 2

输出: 4->5->1->2->3->NULL

解题思路

如果能有链表的长度,就不用担心k非常大而不断的循环旋转了。所谓的旋转其实就是在链表中间断开,首尾相连。在获取链表长度的时候顺便把链表的首尾连起来。注意断开的位置是在倒数第k个之前。

AC源码

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

    def myprint(self):
        print(self.val)
        if self.next:
            self.next.myprint()


class Solution(object):
    def rotateRight(self, head, k):
        """ :type head: ListNode :type k: int :rtype: ListNode """
        if not head:
            return []
        curr = head
        length = 1
        while curr.next:
            curr = curr.next
            length += 1
        curr.next = head
        cur = head
        shift = length - k % length
        while shift > 0:
            curr = curr.next
            shift -= 1
        result = curr.next
        curr.next = None
        return result


if __name__ == "__main__":
    l1 = ListNode(1)
    l2 = ListNode(2)
    l3 = ListNode(3)
    l4 = ListNode(4)
    l5 = ListNode(5)
    l1.next = l2
    l2.next = l3
    l3.next = l4
    l4.next = l5
    result = Solution().rotateRight(l1, 2)
    result.myprint()

欢迎查看我的Github (https://github.com/gavinfish/LeetCode-Python) 来获得相关源码。

你可能感兴趣的:(LeetCode,算法,python,链表,旋转)