Why did time limit exceed


  • 0
    F

    I'm having trouble figuring out why my solution exceeds time limit. It is O(n). I've also seen one post with similar question. Below is the code:

       '''
        if head is None or head.next is None:
            return head
    
        node_pre = head
        head = head.next
        while head is not None:
            node_next = head.next
            head.next = node_pre
            node_pre = head
            head = node_next
        return node_pre

  • 1
    G

    You forgot to set head->next to NULL!
    Then there was a cycle in linked list.


  • 0
    F

    Thanks! It's working now


Log in to reply
 

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