Why does this exceed the time limit?


  • 0
    R
        if head is None or head.next is None or k == 0: 
            return head
        
     
        size = 1 
        last2, last = None, head
        while last:
            size += 1
            if last.next.next is None:  #at second last position
                last2 = last
                last = last2.next
                break
            else:
                last = last.next
        
        k = k%size  #remove redundant rotates
        for _ in range(k):  #rotate k number of times
            last.next = head
            last2.next = None
            head = last
        return head

Log in to reply
 

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