Clear Python solution


  • 0
    class Solution(object):
        def rotateRight(self, head, k):
            """
            :type head: ListNode
            :type k: int
            :rtype: ListNode
            """
            if head is None:
                return head
            l, p = 1, head
            while p.next:
                p = p.next
                l += 1
            k = k % l
            if k == 0:
                return head
            
            q, n = head, l - k - 1
            while n:
                q = q.next
                n -= 1
            
            p.next = head
            head = q.next
            q.next = None
            return head

Log in to reply
 

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