Linked List Cycle
Start two pointers at head. Each move, slow goes 1 step and fast goes 2 steps. If the list ends, fast reaches null first and we return false. If there is a loop, both pointers end up going round it, and fast gets 1 node closer to slow every move, so it must land exactly on slow. Then we return true.
There is a cycle exactly when slow and fast land on the same node; if fast or fast->next becomes null, the list has an end.
Problem
You are given head, the first node of a singly linked list.
The list has a cycle if you can keep following next pointers forever, because some node's next points back to a node that came earlier. Return true if the list has a cycle, and false if following next eventually reaches null.
pos in the examples is only used to build the test list: it is the index of the node the tail links back to (-1 means no cycle). Your function does not receive it.
head — the first node of the list (it can be null).
true if the list contains a cycle, otherwise false.
The tail (-4) links back to the node at index 1 (value 2), so 2 → 0 → -4 → 2 → … repeats forever.
The tail (2) links back to the head (1).
One node whose next is null. The walk ends, so there is no cycle.
- • The number of nodes is in the range [0, 10^4]
- • −10^5 ≤ Node.val ≤ 10^5
- • pos is −1 or a valid index in the list
- • Follow-up: can you solve it using O(1) extra memory?
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
If there is no cycle, what does the walk along
nexteventually reach? - 2.
If there is a cycle, what happens to that walk instead?
- 3.
To notice a repeat, what exactly should I remember about each node — its value, or the node itself?
- 4.
Could two different nodes hold the same value? What would that do to a check based on values?
- 5.
Without any memory, how could two walkers tell me they are going round in a circle?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Hash set of node pointers; seeing a node twice means a cycle | O(N) | O(N) |
| Optimal | slow moves 1, fast moves 2; they meet only if there is a cycle | O(N) | O(1) |
Why does this belong to this pattern?
- “determine if the list has a cycle”We only need yes/no, not where the loop is → two pointers can answer it.
- “linked list, follow next”Each node has exactly one next, so the walk is a path that either ends or bends into one loop (a "ρ" shape).
- “O(1) memory follow-up”No visited set → let a fast and a slow pointer find the repeat for us.
This is the anchor of the cycle sub-pattern: the plainest form of Floyd, where we only check whether they meet. The later questions reuse the same meeting: Happy Number runs it on numbers instead of nodes, Find Length of Loop walks once round from the meeting node, and Linked List Cycle II adds a second phase to find where the loop starts.
Don't make this mistake
Different nodes can hold the same value. On [7, 7, 7] with no cycle, a value set sees 7 twice and wrongly says "cycle". A cycle means the same node comes back, so store the pointer.
For an empty list head is null, so fast->next crashes straight away. Always check fast before you read fast->next. The order matters: && stops at the first false part.
Both start at head, so they are equal before the first move and this returns true for every list with 2+ nodes. Move first, then compare.
This is the pattern-confusion trap: a normal list walk assumes the list ends. With a cycle it never reaches null and loops forever. Whenever a walk might repeat, you need either a visited set or a second, faster pointer to detect it.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Linked List — Fast & Slow translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.