Easy Python Solution with Explanation

  • 0

    The question is to rotate the list, so just make it circled firstly. Then the question is much easier, just rotate k position.

    class Solution(object):
        def rotateRight(self, head, k):
            if not head:
            l = 0
            p = head
            while p.next:
                l += 1
                p = p.next
            l += 1
            p.next = head
            i = l - k%l
            while i > 0:
                p = p.next
                i -= 1
            head = p.next
            p.next = None
            return head

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.