Java Programs›Collections›Detect a Cycle in a Linked List

Detect a Cycle in a Linked List in Java

advanced·  Collections  ·  Linked List

Problem

A linked list has a cycle when some node's next pointer leads back to an earlier node, so walking the list never reaches the end.

Given the head of a singly linked list, decide whether it contains a cycle and, if it does, find the node where the cycle starts.

Input
1 → 2 → 3 → 4 → 5 → 6 → back to 3
Output
cycle starts at node 3

Java Program

Java
public class DetectCycleLinkedList { static class Node { int value; Node next; Node(int value) { this.value = value; } } // Floyd's algorithm: returns the node where the cycle starts, or null if there is none static Node findCycleStart(Node head) { Node slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next; // one step fast = fast.next.next; // two steps if (slow == fast) { // they met inside the cycle Node p = head; while (p != slow) { // move both one step at a time; they meet at the entry p = p.next; slow = slow.next; } return p; } } return null; // fast reached the end, so the list has an end } static Node buildList(int count) { Node head = new Node(1), tail = head; for (int v = 2; v <= count; v++) { tail.next = new Node(v); tail = tail.next; } return head; } public static void main(String[] args) { Node straight = buildList(6); System.out.println("List 1 has a cycle: " + (findCycleStart(straight) != null)); Node looped = buildList(6); Node third = looped.next.next; Node last = third.next.next.next; last.next = third; // 6 points back to 3 Node start = findCycleStart(looped); System.out.println("List 2 has a cycle: " + (start != null)); System.out.println("Cycle starts at node: " + start.value); } }

Output

List 1 has a cycle: false List 2 has a cycle: true Cycle starts at node: 3

Core Logic

A slow pointer moves one step and a fast pointer two; if there's a cycle the fast one eventually laps the slow one inside it, and if not, the fast one falls off the end.

How It Works
  1. 1Both pointers start at the head.
  2. 2The loop advances them until fast or fast.next is null (no cycle) or they land on the same node (cycle).
  3. 3After they meet, a new pointer starts from the head while slow keeps going, both one step at a time.
  4. 4The node where those two meet is the start of the cycle.
In the looped list, the pointers meet inside the loop 3 → 4 → 5 → 6, and the second phase brings both to node 3.
💡

Key Point: The pointers must be compared with == (same node), not by value — two different nodes can hold equal values.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

Why: The fast pointer catches up within a number of steps proportional to the list length, and only two or three references are used.

Key Concepts

two pointersfast and slow pointersFloyd's algorithm

Approach 2: Using a HashSet

Java
import java.util.HashSet; import java.util.Set; public class DetectCycleHashSet { static class Node { int value; Node next; Node(int value) { this.value = value; } } public static void main(String[] args) { Node head = new Node(1); Node tail = head; for (int v = 2; v <= 6; v++) { tail.next = new Node(v); tail = tail.next; } tail.next = head.next.next; // 6 points back to 3 Set<Node> seen = new HashSet<>(); // node identity, not value Node current = head; while (current != null && seen.add(current)) { // add() returns false for a repeat current = current.next; } if (current == null) { System.out.println("No cycle"); } else { System.out.println("Cycle starts at node: " + current.value); System.out.println("Nodes visited before the repeat: " + seen.size()); } } }

Output

Cycle starts at node: 3 Nodes visited before the repeat: 6

Core Logic

Remembering every node already visited makes a cycle obvious: the first node seen twice is where the loop begins.

How It Works
  1. 1The list is walked from the head.
  2. 2seen.add(current) returns false when the node is already in the set.
  3. 3Reaching null means the list ends, so there is no cycle.
  4. 4Otherwise current is the first repeated node — the start of the cycle.
Nodes 1 to 6 are added, then the walk returns to node 3, which is already in the set.
💡

Key Point: Simple and easy to explain, but it stores every node — Floyd's algorithm gives the same answer with constant extra memory, which is what interviewers usually push for.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: Each node is visited once and stored in the set.

Key Concepts

HashSetidentity-based tracking

Interview Questions

How do you find the length of the cycle?

After the slow and fast pointers meet, keep one still and move the other until it returns, counting the steps.

How do you remove the cycle?

Find the node where the cycle starts, then walk around the loop to the node whose next points back to it, and set that next to null.

Learn the Concept

Related Programs