Cycle detection in a linked list Exhaustive theory
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Cycle detection in a linked list determines whether a sequence of nodes eventually loops back to a previously visited node. The two primary algorithms are the hash set approach (O(n) time, O(n) space) and Floyd's tortoise and hare algorithm (O(n) time, O(1) space). Floyd's algorithm is the standard interview solution because it uses constant space. Beyond detection, these algorithms can find the cycle start node and compute the cycle length.
What Is a Cycle?
A cycle exists when a node's next pointer points back to a previous node, creating an infinite loop:
Node 4's next points to node 2, so traversing from node 2 onward loops forever (2 → 3 → 4 → 2 → ...).
Algorithm 1: Hash Set
Store visited nodes in a set. If you encounter a node already in the set, a cycle exists:
- Time complexity: O(n) — visit each node at most once
- Space complexity: O(n) — store up to n node references
Algorithm 2: Floyd's Tortoise and Hare (Optimal)
Use two pointers: slow (moves 1 step) and fast (moves 2 steps). If a cycle exists, fast catches slow inside the cycle:
- Time complexity: O(n) — fast enters cycle, gap closes by 1 each step
- Space complexity: O(1) — only two pointers
Why It Works
When both pointers are inside a cycle of length C:
- Each step, fast gains 1 node on slow (fast moves 2, slow moves 1)
- The gap between them decreases by 1 per step
- After at most C steps, fast catches slow (gap becomes 0)
Finding the Cycle Start Node
After detection, find where the cycle begins:
Mathematical Proof
Let:
L= distance from head to cycle startC= cycle lengthK= distance from cycle start to meeting point
When they meet: slow traveled L + K steps, fast traveled 2(L + K) steps.
Fast also made extra laps: 2(L + K) - (L + K) = L + K extra nodes = multiple of C.
So L + K = nC for some integer n, meaning L = nC - K.
Moving L steps from the meeting point lands at the cycle start.
Finding the Cycle Length
After detecting the meeting point, count the cycle length:
Java Implementation
C++ Implementation
Algorithm Comparison
| Algorithm | Time | Space | Detects Start | Cycle Length |
| Hash set | O(n) | O(n) | Yes (first revisited node) | Yes |
| Floyd's (tortoise/hare) | O(n) | O(1) | Yes (with phase 2) | Yes |
| Brent's | O(n) | O(1) | Yes | Yes |
| Node marking (destructive) | O(n) | O(1) | Yes | No |
Common Pitfalls
- Not checking
fast.nextbefore advancing fast by 2: Iffastis on the last node,fast.next.nextdereferences null. Always check bothfast != nullandfast.next != nullin the while condition. - Comparing node values instead of node references: Cycle detection requires checking if two pointers reference the same node object (
slow == fast), not if they have the same value (slow.val == fast.val). Different nodes can have identical values. - Assuming a cycle always starts at the head: The cycle start can be any node in the list. The tail node's
nextmay point to the head, the middle, or even itself. Floyd's phase 2 handles all cases. - Modifying the list during detection: Some approaches mark visited nodes by changing their values or pointers. This is destructive and makes the list unusable afterward. Floyd's algorithm is non-destructive.
- Forgetting the
elseclause on the while loop for phase 2: In Python, if the while loop completes without breaking (no cycle), you must returnNonebefore executing phase 2. Without this check, phase 2 runs on a non-cyclic list and produces incorrect results.
Summary
- Floyd's tortoise and hare algorithm detects cycles in O(n) time and O(1) space
- Slow pointer moves 1 step, fast pointer moves 2 steps — they meet inside the cycle if one exists
- To find the cycle start: reset one pointer to head, advance both by 1 step until they meet
- Cycle length: after detection, count steps until you return to the meeting point
- Always check
fast != null && fast.next != nullto prevent null pointer errors

