Linked Lists: Detect a Cycle

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