Fast and slow pointers (Floyd's "tortoise and hare") walk the same sequence at different speeds, typically one step and two steps at a time. If the sequence loops, the fast pointer must eventually lap the slow one; if it ends, the fast pointer gets there first. A little arithmetic on where they meet also reveals where the cycle starts and how long it is, all with O(1) extra memory instead of a hash set of visited nodes.
Think of it like
Two runners on a track
On a straight road, a fast runner simply finishes first. On a circular track, the fast runner keeps gaining and eventually laps the slow one. So if two runners start together and the fast one ever catches the slow one from behind, the course must contain a loop. Floyd's algorithm is that observation, plus a clever way to find where the loop begins.
How to recognize it
| If the problem says... | Think... |
|---|---|
| Does this linked list have a cycle? Where does it start? | Floyd's cycle detection, phase 1 and phase 2 |
| Find the middle node, or split a list in half | Slow moves 1, fast moves 2; slow is the middle when fast ends |
A sequence x → f(x) on a finite set (digit squares, index jumps) | It must eventually repeat: treat it as an implicit linked list |
| Find the duplicate without modifying the array, O(1) space | Array as a linked list, i → nums[i] |
| Palindrome linked list, reorder list | Find the middle first, then reverse half |
Why the pointers must meet
If there is no cycle, the fast pointer reaches the end (null) after about n/2 steps and we stop. If there is a cycle, both pointers eventually enter it. From then on, think of the distance from slow forward to fast around the loop: each step, slow moves 1 and fast moves 2, so the fast pointer closes the gap by exactly one node per step.
A gap that shrinks by exactly one cannot jump over zero, so they land on the same node within at most L steps after slow enters the cycle, where L is the cycle length. The visualization above shows this: once both pointers are on the loop, the gap between them ticks down by one each step.
Finding the cycle start
- a
- Number of steps from the head to the first node of the cycle (the entrance).
- b
- Number of steps from the entrance forward to the meeting point, with 0 ≤ b < L.
- L
- Length of the cycle.
When they meet, slow has walked a + b steps. It has not finished a lap yet, because fast catches it within L steps of it entering the loop. (One exception: when a = 0 both start inside the cycle and first meet back at the head after exactly L steps; the extra lap only changes k below, so the conclusion still holds.) Fast has walked twice as far, and the extra distance is a whole number of laps:
Read the last line out loud: walking a steps from the meeting point is the same as walking L - b steps (which brings you from the meeting point forward to the entrance) plus some whole laps. So put one pointer back at the head, leave the other at the meeting point, and advance both one step at a time. After exactly a steps, both stand on the entrance.
- 1
Phase 1: detect
slow = fast = head. Repeatslow = slow.next,fast = fast.next.nextuntil they meet (cycle) or fast hitsnull(no cycle). - 2
Phase 2: find the entrance
Set
slow = head, keepfastat the meeting point. Move both by one until they meet. That node is the cycle start. - 3
Phase 3 (optional): measure the cycle
From any node on the cycle, walk until you return to it, counting steps. That count is
L.
| Quantity | Value in the example |
|---|---|
| List | 0 → 1 → 2 → 3 → 4 → 5 → back to 2 |
| a (head to entrance) | 2 |
| L (cycle length) | 4 (nodes 2, 3, 4, 5) |
| Phase 1 meeting point | After 4 steps, slow at node 4 and fast at node 4 (b = 2) |
| Check: a = (k − 1)L + (L − b) | 2 = 0 × 4 + (4 − 2) with k = 1 |
| Phase 2 | head → 1 → 2 and 4 → 5 → 2: both reach node 2 after 2 steps |
Middle of a linked list
Move slow by one and fast by two. When fast cannot move any further, slow has covered half the distance and sits on the middle. The loop condition decides which middle you get for even lengths:
| Loop condition | Odd length (1→2→3→4→5) | Even length (1→2→3→4) |
|---|---|---|
while fast and fast.next | 3 | 3 (the second middle) |
while fast.next and fast.next.next | 3 | 2 (the first middle) |
Implicit linked lists
Happy number
Replace a number with the sum of the squares of its digits and repeat. The number is "happy" if this reaches 1. Any number with four or more digits maps to a smaller number, so the sequence stays bounded and must eventually repeat: either it reaches the fixed point 1 (1 → 1) or it falls into the unhappy loop 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4. Run fast and slow over next(x) and check whether they meet at 1.
Find the duplicate number
An array of n + 1 integers holds values in [1, n], so some value repeats. Treat each index as a node with an edge i → nums[i]. Index 0 has no incoming edge (no value is 0), so it is a safe starting point on a "tail". The duplicated value has two incoming edges, which is exactly what a cycle entrance looks like. Phase 2 of Floyd's algorithm finds it.
| Step | slow | fast | Note |
|---|---|---|---|
| start | 0 | 0 | nums = [1, 3, 4, 2, 2], edges 0→1→3→2→4→2 |
| 1 | 1 | 3 | |
| 2 | 3 | 4 | |
| 3 | 2 | 4 | |
| 4 | 4 | 4 | Phase 1 meets at index 4 |
| phase 2 | 0 → 1 → 3 → 2 | 4 → 2 → 4 → 2 | Both at 2 after 3 steps: the duplicate is 2 |
Palindrome linked list
- 1
Find the middle
Use the first-middle variant so the first half is never shorter than the second.
- 2
Reverse the second half
Reverse the list starting at
slow.nextin place, O(1) space. - 3
Compare
Walk from the head and from the head of the reversed half together; any mismatch means not a palindrome.
- 4
Restore
Reverse the second half again and reattach it. Callers do not expect a query to mutate their list.
Fast and slow pointers vs a hash set
Hash set of visited nodes
- Simple: stop at the first node seen twice, which is also the cycle start
- O(n) time, O(n) extra space
- Works for any graph shape, not just functional (one-successor) graphs
Floyd's tortoise and hare
- O(n) time, O(1) extra space
- Needs each node to have exactly one successor
- Also gives the middle node, the cycle start and the cycle length
Edge cases
- Empty list or a single node without a self-loop: no cycle. A single node pointing to itself is a cycle of length 1.
- Always check
fastandfast.nextbefore taking two steps, or you will dereferencenull. - Compare node identity, not values: two different nodes can hold the same value.
- In phase 2, both pointers must move one step at a time.
Complexity
| Task | Time | Space | Notes |
|---|---|---|---|
| Detect a cycle | O(n) | O(1) | Meeting within L steps after slow enters the loop |
| Find the cycle start | O(n) | O(1) | Phase 2 takes exactly a steps |
| Cycle length | O(L) | O(1) | One lap from the meeting point |
| Middle of a list | O(n) | O(1) | n/2 iterations |
| Find the duplicate number | O(n) | O(1) | Array as linked list |
| Hash-set alternative | O(n) | O(n) | Simpler but uses memory |
Implementation
Phase 1 finds a meeting point, phase 2 finds the entrance, and one more lap measures the cycle.
class ListNode:
def __init__(self, val: int = 0, next: "ListNode | None" = None):
self.val = val
self.next = next
def detect_cycle(head: ListNode | None) -> ListNode | None:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow is fast: # phase 1: they met inside the cycle
slow = head # phase 2: one pointer back to the head
while slow is not fast:
slow = slow.next
fast = fast.next
return slow # the cycle entrance
return None
def cycle_length(head: ListNode | None) -> int:
entrance = detect_cycle(head)
if entrance is None:
return 0
length, node = 1, entrance.next
while node is not entrance:
node = node.next
length += 1
return lengthKey takeaways
- Two pointers moving at speeds 1 and 2 meet if and only if the sequence has a cycle, using O(1) extra space.
- From
a = (k - 1)L + (L - b): restart one pointer at the head, move both by one, and they meet at the cycle entrance. - The cycle length is one lap from any node on the cycle.
- When fast reaches the end, slow is at the middle; the loop condition picks the first or second middle.
- Any function
x → f(x)on a finite set is an implicit linked list: happy numbers and Find the Duplicate Number use the same algorithm.
Common pitfalls
- Dereferencing
fast.next.nextwithout checkingfastandfast.nextfirst. - Moving fast by two in phase 2; both pointers must move by one.
- Comparing node values instead of node identity when detecting the meeting point.
- Leaving the list reversed after a palindrome check, which mutates the caller's data.
Interview tips
- Start with the hash-set solution, then offer Floyd's as the O(1)-space follow-up and prove the phase-2 identity.
- For Find the Duplicate Number, explain why index 0 is outside the cycle and why the duplicate is the entrance.
- Know which middle your loop condition returns for even-length lists; reorder and sort problems depend on it.
Real-world applications
- Detecting infinite loops in iterated functions and state machines.
- Pollard's rho integer factorization and discrete-log algorithms (cycle finding over
x² + c mod n). - Measuring the period of pseudo-random number generators.
- Detecting corrupted, circular linked structures in memory or on disk.
Practice problems
Test yourself
After the pointers meet in phase 1, why does resetting one pointer to the head and moving both by one step find the cycle start?
Related topics
Finished this lesson? Track it to see your progress on the roadmap.