Clean Python Solution


  • 0
    W
    class Solution(object):
        
        def rotateRight(self, head, k):
            if not head:
                return head
                
            length = self.length(head)
            k = k % length
            if k == 0:
                return head
            
            fast = slow = head
            while k:
                fast = fast.next
                k -= 1
            
            while fast.next:
                fast = fast.next
                slow = slow.next
            
            newHead = slow.next
            slow.next = None
            fast.next = head
            return newHead
    
        def length(self, head):
            count = 0
            while head:
                count += 1
                head = head.next
            return count
    

Log in to reply
 

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