Linked Lists: Detect a Cycle

Sort by

recency

|

515 Discussions

|

  • + 0 comments

    @hakerRank, why the algorithm in ruby not working?

  • + 0 comments

    Seems like the starter code and tests have become corrupt along the way. The started code does not match the challenge description at all. I tried the Ruby, Java 8, and Java 15 examples and they were all the same way.

  • + 0 comments

    Has anyone tried using Javascript as the language ? The input to the test cases is not a pointer to the list, instead its just a string "1".

  • + 1 comment

    Here's one way to do it in Python3:

    def has_cycle(head):
        slowPointer = head
        fastPointer = head
        while fastPointer and fastPointer.next:
            if slowPointer == fastPointer:
                return True
            slowPointer = slowPointer.next
            fastPointer = fastPointer.next.next
        return False 
    
  • + 0 comments

    python3

    def has_cycle(head):
        slow, fast = head, head
        
        while fast and fast.next:
            
            slow = slow.next
            fast = fast.next.next
            if slow == fast:
                return True
            
        return False