Linked Lists
Fast & Slow Pointers (Floyd's)
Master the runner technique to find list midpoints, detect cycles, and locate cycle start nodes in O(1) space.
Last Updated: August 2, 2026
•
15 min read
1. Introduction
What is the Fast & Slow Pointer Pattern?
The Fast & Slow Pointer Pattern (also known as Floyd's Cycle-Finding Algorithm or the Runner Technique) uses two pointer references that traverse a linked list at different speeds:Why is it Important?
This technique allows you to detect cycles (loops) and identify structural milestones (like finding the exact middle node of a list) inO(N) time and O(1) space. Without it, cycle detection would require storing seen node references in a HashSet, taking O(N) auxiliary memory.
Where is it Used?
2. Mental Model: Tortoise and Hare on a Circular Track
Imagine a tortoise (slow) and a hare (fast) racing:
3. Core Algorithms & Implementations
1. Middle of the Linked List (LeetCode 876)
slow and fast pointers at head.fast by 2 steps (fast = fast.next.next) and slow by 1 step (slow = slow.next) while fast != null and fast.next != null.fast reaches the end, slow will point to the exact middle node of the list.2. Linked List Cycle Detection (LeetCode 141)
slow and fast pointers meet (slow == fast), a cycle is present.fast or fast.next becomes null, no cycle exists.3. Finding the Start of the Cycle (LeetCode 142)
slow and fast meet, keep fast at the meeting point and place slow back at head.4. Mathematical Proof: Floyd's Cycle Detection Start
Let's understand why resetting one pointer to head and advancing both 1 step at a time locates the cycle starting node:
= A + B.= A + B + N × L (where N is the number of cycles run).2(A + B) = A + B + N × L \implies A + B = N × L \implies A = N × L - B
L = B + C, so:A = (N - 1) × L + C
A from the head to the cycle start is equivalent to the distance C from the meeting point back to the cycle start (plus any full cycles). Thus, moving both pointers 1 step at a time guarantees they will meet at the cycle start!5. Real-World Applications
6. Interview Perspective
How Interviewers Ask This Topic
Interviewers test pointer-chasing mechanics:Common Mistakes
Warning: 1. Null Pointer Exception in fast.next: Writing
while (fast.next != null && fast != null) is incorrect because fast could be null, making fast.next crash. Always check fast != null first.> 2. Wrong Midpoint for Even Lists: If a list has 6 nodes (1 -> 2 -> 3 -> 4 -> 5 -> 6), fast/slow ends with slow at4(the second middle). If you want the first middle (3), adjust loop bounds.
7. Summary
8. Quiz
Question 1: Why does the fast pointer skip 2 nodes instead of 3?
Answer: If the fast pointer skipped 3 nodes, it might skip over the slow pointer entirely inside the cycle without landing on the exact same node, which would cause an infinite loop in our detection code. Advancing by 2 guarantees they will eventually meet.Question 2: What is the space complexity of Floyd's Cycle-Finding Algorithm?
Answer:O(1) auxiliary space. We only allocate two pointer variables (slow and fast), regardless of list size.
Question 3: In finding the midpoint of a list with 5 nodes (1 -> 2 -> 3 -> 4 -> 5), where does the slow pointer stop?
Answer: Node3 (the exact middle).