Sort by

recency

|

1343 Discussions

|

  • + 0 comments

    Python code for Cycle detection

    **Works Totally well for all the test cases **

    def has_cycle(head):
        if not head or not head.next:
            return 0      
        p = q = head
        while(True):
            p = p.next 
            q = q.next
            q = q.next if (q) else None
            if(p and q and p != q):
                continue
            else:
                break
        if( p == q ):
            return 1    
        else:
            return 0    
    

    Upvote it if you found it pretty helpful & Do Comment below for any suggestions⛳

  • + 0 comments

    Description is BS!

    Head refers to the list of nodes 1 -> 2 -> 3 -> 1 -> NULL There is a cycle where node 3 points back to node 1, so return .

    Ah??? If there is a cycle then there will be no NULL and it should be like so:

    1 -> 2 -> 3 -> 1 -> 2 ... etc

  • + 0 comments

    This is not a well written problem. This is bad.

  • + 0 comments

    Hacky solution for the lulz:

    def has_cycle(head):
        c = 0
        while head:
            head = head.next
            c += 1
            if c > 1000:
                return 1
        return 0
    
  • + 0 comments
     if head is None:
            return False
        
        slow = fast = head
        
        while fast is not None and fast.next is not None:
            slow = slow.next
            fast = fast.next.next
            
            if slow == fast:
                return True
        return False