advanced · linked lists · fast and slow pointers · cycle detection
Where does the linked list loop back?
Problem
A singly linked list is stored as an array next: node i points to node next[i], and −1 means "no next node". The list starts at node 0. Following the pointers either ends at −1 or enters a loop.
Return the first node of the loop (the node reached twice first when walking from node 0), or −1 if the walk ends. Use O(1) extra space: no visited set.
Constraints
- 1 ≤ n ≤ 20
- every next[i] is −1 or a node index 0 … n − 1
Examples
next = [1, 2, 3, 4, 2] → 2 — 0 → 1 → 2 → 3 → 4 → 2: the loop starts at 2.
next = [1, 2, -1] → -1
Show the solution
Floyd's tortoise and hare
Inside a loop the fast pointer gains one node per step on the slow one, so it must catch it. The meeting point then sits exactly as far from the entry (going forward) as node 0 is from the entry, modulo the loop length.
Why it is correct
Let μ be the distance from node 0 to the entry and λ the loop length. When they meet, slow has walked k steps and fast 2k, and 2k − k = k is a multiple of λ. From the meeting point, walking μ more steps puts slow at μ + k steps from node 0, which is the entry plus a whole number of loops: the entry. A pointer starting at node 0 also reaches the entry after μ steps, so the two meet first there.
Complexity
Time O(n), extra space O(1). Slow enters the loop after μ steps and is caught within λ more; the second phase takes μ steps. μ + λ ≤ n.
For contrast: Walk from node 0 marking visited nodes; the first node seen twice is the entry. That costs O(n) time and O(n) space.
Edge cases
- A node pointing to itself: the loop has length 1.
- The whole list is a loop: the entry is node 0.
- No loop: −1.
- Nodes that are never reached from node 0 do not matter.
Common mistakes
- Checking fast.next before fast, and reading next[−1].
- Restarting both pointers at node 0 in the second phase.
Step through it
The solution in four languages
Each is checked against one shared set of cases and a randomized comparison with a brute-force reference.
namespace Bekadan.Dsa.Examples.Problems;
public static class ListCycleEntry
{
/// <summary>next[i] is node i's successor (-1 = none); the list starts at node 0. First node of the loop, or -1. Floyd: O(n) time, O(1) space.</summary>
public static int Find(int[] next)
{
int slow = 0, fast = 0;
while (true)
{
if (fast == -1 || next[fast] == -1) return -1; // fast fell off the end
slow = next[slow];
fast = next[next[fast]];
if (slow == fast) break;
}
slow = 0;
while (slow != fast)
{
slow = next[slow];
fast = next[fast];
}
return slow;
}
}
Tested on .NET 10 (SDK 10.0.403), C# 14 · examples/csharp/Bekadan.Dsa.Examples/Problems/ListCycleEntry.cs
from collections.abc import Sequence
def find(nxt: Sequence[int]) -> int:
"""nxt[i] is node i's successor (-1 = none); the list starts at node 0. First node of the loop, or -1. Floyd: O(n), O(1)."""
slow = fast = 0
while True:
if fast == -1 or nxt[fast] == -1:
return -1 # fast fell off the end
slow = nxt[slow]
fast = nxt[nxt[fast]]
if slow == fast:
break
slow = 0
while slow != fast:
slow = nxt[slow]
fast = nxt[fast]
return slow
Tested on CPython 3.14.0 · examples/python/dsa_examples/problems/list_cycle_entry.py
package dev.bekadan.dsa.problems;
public final class ListCycleEntry {
private ListCycleEntry() {
}
/** next[i] is node i's successor (-1 = none); the list starts at node 0. First node of the loop, or -1. Floyd: O(n), O(1). */
public static int find(int[] next) {
int slow = 0;
int fast = 0;
while (true) {
if (fast == -1 || next[fast] == -1) {
return -1; // fast fell off the end
}
slow = next[slow];
fast = next[next[fast]];
if (slow == fast) {
break;
}
}
slow = 0;
while (slow != fast) {
slow = next[slow];
fast = next[fast];
}
return slow;
}
}
Tested on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/main/java/dev/bekadan/dsa/problems/ListCycleEntry.java
#pragma once
#include <cstddef>
#include <span>
namespace bekadan::dsa::problems {
// next[i] is node i's successor (-1 = none); the list starts at node 0. First node of the loop, or -1. Floyd: O(n), O(1).
inline int list_cycle_entry(std::span<const int> next) {
const auto at = [&](int node) { return next[static_cast<std::size_t>(node)]; };
int slow = 0, fast = 0;
while (true) {
if (fast == -1 || at(fast) == -1) return -1; // fast fell off the end
slow = at(slow);
fast = at(at(fast));
if (slow == fast) break;
}
slow = 0;
while (slow != fast) {
slow = at(slow);
fast = at(fast);
}
return slow;
}
} // namespace bekadan::dsa::problems
Tested on C++23, GCC 15.3 (libstdc++) · examples/cpp/include/bekadan/dsa/problems/list_cycle_entry.hpp