Why this code is Time Limit Exceeded?


  • 0
    H

    Hello. Why this code is Time Limit Exceeded?
    In case, It can executed in my computer.
    Please answer this problem.

    Last executed input: {-1,-7,7,-4,19,6,-9,-5,-2,-5}, tail connects to node index 9

    def hasCycle(self, head):
        t = head
        n = head
        if n is None:
            return False
    
        if n.next is head:
            return True
    
        while n.next is not None:
            n = n.next
            while t is not n:
                if t is n.next:
                    return True
                t = t.next
            t = head
        return False

Log in to reply
 

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