solution without counting the length


  • 0
    Y
    class Solution(object):
        def rotateRight(self, head, k):
            """
            :type head: ListNode
            :type k: int
            :rtype: ListNode
            """
            if head is None:
                return
            dis=0
            node=head
            while node.next and dis<k:
                node=node.next
                dis+=1
            if dis==k:
                tail=head
                while node.next:
                    node=node.next
                    tail=tail.next
                node.next=head
                head=tail.next
                tail.next=None
                return head
            else:
                return self.rotateRight(head,k%(dis+1))
    

Log in to reply
 

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