Linked Lists: Detect a Cycle

  • + 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