Fast & Slow Pointers
Floyd's tortoise and hare — detect cycles, find the cycle start, the middle node, and duplicates in O(1) space.
Two pointers traverse the same sequence at different speeds — typically slow moves 1 step and fast moves 2. If there's a cycle, fast laps slow and they meet. If there's no cycle, fast reaches the end. When fast reaches the end of a list, slow is in the middle.
- Detect a cycle in a linked list or in any "next" function (
n → f(n)). - Find the middle of a linked list in one pass.
- Find the start of a cycle, or the cycle length.
- Find a duplicate number in an array treated as a function graph.
- You're required to use O(1) extra space (otherwise a hash set also works).
Cycle detection and cycle start
boolean hasCycle(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) return true;
}
return false;
}
ListNode detectCycleStart(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) { // meeting point inside the cycle
ListNode p = head;
while (p != slow) { p = p.next; slow = slow.next; }
return p; // both arrive at the cycle start
}
}
return null;
}def has_cycle(head):
slow = fast = head
while fast and fast.next:
slow, fast = slow.next, fast.next.next
if slow is fast:
return True
return False
def detect_cycle_start(head):
slow = fast = head
while fast and fast.next:
slow, fast = slow.next, fast.next.next
if slow is fast:
p = head
while p is not slow:
p, slow = p.next, slow.next
return p
return Nonefunction detectCycleStart(head: ListNode | null): ListNode | null {
let slow = head, fast = head;
while (fast && fast.next) {
slow = slow!.next;
fast = fast.next.next;
if (slow === fast) {
let p = head;
while (p !== slow) { p = p!.next; slow = slow!.next; }
return p;
}
}
return null;
}Why resetting one pointer finds the cycle start
Let the distance from head to the cycle start be a, the meeting point be b steps into the cycle, and the cycle length c. Slow walked a + b; fast walked twice that, i.e. a + b + kc for some k. So a + b = kc, meaning a = kc − b: walking a steps from the meeting point lands exactly on the cycle start — and so does walking a steps from the head.
Middle of a linked list
ListNode middle(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow; // second middle for even lengths
}def middle(head):
slow = fast = head
while fast and fast.next:
slow, fast = slow.next, fast.next.next
return slowfunction middle(head: ListNode | null): ListNode | null {
let slow = head, fast = head;
while (fast && fast.next) { slow = slow!.next; fast = fast.next.next; }
return slow;
}Middle-finding powers: palindrome linked list (find middle, reverse the second half, compare), reorder list (middle, reverse, interleave), and merge sort on lists.
Beyond linked lists: cycles in functions
Any function f on a finite set eventually cycles. Happy number: repeatedly replace n with the sum of squares of its digits — fast/slow detects whether you loop without reaching 1.
Find the duplicate number (array of n + 1 values in 1..n, read-only, O(1) space): treat i → nums[i] as a linked list. The duplicate value is the entry point of the cycle.
int findDuplicate(int[] nums) {
int slow = nums[0], fast = nums[0];
do { slow = nums[slow]; fast = nums[nums[fast]]; } while (slow != fast);
slow = nums[0];
while (slow != fast) { slow = nums[slow]; fast = nums[fast]; }
return slow;
}def find_duplicate(nums):
slow = fast = nums[0]
while True:
slow, fast = nums[slow], nums[nums[fast]]
if slow == fast:
break
slow = nums[0]
while slow != fast:
slow, fast = nums[slow], nums[fast]
return slowfunction findDuplicate(nums: number[]): number {
let slow = nums[0], fast = nums[0];
do { slow = nums[slow]; fast = nums[nums[fast]]; } while (slow !== fast);
slow = nums[0];
while (slow !== fast) { slow = nums[slow]; fast = nums[fast]; }
return slow;
}- Linked List Cycleeasy
- Middle of the Linked Listeasy
- Happy Numbercycle in digit-square functioneasy
- Palindrome Linked Listmiddle + reverse second halfeasy
- Linked List Cycle IIcycle startmedium
- Reorder Listmiddle, reverse, mergemedium
- Remove Nth Node From End of Listgap of nmedium
- Find the Duplicate Numberindex → value graphmedium
- Circular Array Loopmedium
- Slow moves 1, fast moves 2: they meet iff there's a cycle.
- To find the cycle start, reset one pointer to the head and move both by 1.
- When fast hits the end, slow is at the middle.
- Works on any "next" function, not just linked lists — all in O(1) space.