Linked Lists: Detect a Cycle

  • + 5 comments

    For anyone wants to compare this with a HashSet solution:

    boolean hasCycle(Node head) {
        Set<Node> seen = new HashSet<>();
        while (head != null) {
            seen.add(head);
            head = head.next;
            if (seen.contains(head)) return true;
        }
        return false;
    }