Fast & Slow Pointers (Floyd's Cycle Detection)
đ Concept
The fast and slow pointer technique (also called Floyd's Tortoise and Hare) uses two pointers moving at different speeds. It's the standard approach for cycle detection and finding middle elements.
Core applications:
- Cycle detection in linked list â fast moves 2, slow moves 1. If cycle exists, they meet.
- Find cycle start â after meeting, reset one pointer to head. Both move 1 step. They meet at cycle start.
- Find middle of linked list â when fast reaches end, slow is at middle.
- Happy number â detect cycles in the number sequence.
- Find duplicate â Floyd's applied to index-value mapping (LeetCode 287).
Why it works (cycle detection):
- If there's a cycle of length C, after slow enters the cycle, fast catches up by 1 step per iteration (fast's relative speed is 1).
- They must meet within C steps after slow enters the cycle.
- Time: O(n), Space: O(1) â no hash set needed!
đ Real-world analogy: Two runners on a circular track â the faster runner will eventually lap the slower one. If the track is not circular (no cycle), the fast runner reaches the end first.
đť Code Example
1// DETECT CYCLE â O(n) time, O(1) space2function hasCycle(head) {3 let slow = head, fast = head;4 while (fast && fast.next) {5 slow = slow.next;6 fast = fast.next.next;7 if (slow === fast) return true; // Cycle detected!8 }9 return false; // Fast reached end â no cycle10}1112// FIND CYCLE START13function detectCycleStart(head) {14 let slow = head, fast = head;15 while (fast && fast.next) {16 slow = slow.next;17 fast = fast.next.next;18 if (slow === fast) {19 // Reset slow to head, move both at speed 120 slow = head;21 while (slow !== fast) {22 slow = slow.next;23 fast = fast.next;24 }25 return slow; // Meeting point = cycle start26 }27 }28 return null;29}3031// FIND MIDDLE OF LINKED LIST32function findMiddle(head) {33 let slow = head, fast = head;34 while (fast && fast.next) {35 slow = slow.next;36 fast = fast.next.next;37 }38 return slow; // Slow is at middle when fast reaches end39}4041// HAPPY NUMBER â Detect cycle in number sequence42function isHappy(n: number): boolean {43 function sumOfSquares(num: number): number {44 let sum = 0;45 while (num > 0) {46 const digit = num % 10;47 sum += digit * digit;48 num = Math.floor(num / 10);49 }50 return sum;51 }5253 let slow = n, fast = n;54 do {55 slow = sumOfSquares(slow);56 fast = sumOfSquares(sumOfSquares(fast));57 } while (slow !== fast);5859 return slow === 1; // 1 â 1 is a cycle of length 160}6162// FIND DUPLICATE NUMBER â Floyd's on array index mapping63function findDuplicate(nums: number[]): number {64 // Treat as linked list: index â value â next index65 let slow = nums[0], fast = nums[0];6667 // Phase 1: Find meeting point68 do {69 slow = nums[slow];70 fast = nums[nums[fast]];71 } while (slow !== fast);7273 // Phase 2: Find entrance (duplicate)74 slow = nums[0];75 while (slow !== fast) {76 slow = nums[slow];77 fast = nums[fast];78 }79 return slow;80}
đď¸ Practice Exercise
Practice Problems:
- Detect if a linked list has a cycle
- Find the start of the cycle in a linked list
- Find the middle node of a linked list
- Check if a number is happy (sum of square of digits â 1)
- Find the duplicate number in [1..n] array with n+1 elements
- Check if a linked list is a palindrome using fast/slow pointers
- Split a linked list into two halves using fast/slow
- Find the intersection node of two linked lists
- Remove the nth node from the end of a linked list in one pass
- Reorder list: L0âLnâL1âLn-1âL2âLn-2...
â ď¸ Common Mistakes
Dereferencing null â always check
fast && fast.nextbefore moving fast two stepsForgetting to check for no-cycle case â fast reaches null means no cycle
Not resetting slow to head in phase 2 of cycle detection â both must move at speed 1
Applying Floyd's to problems without cyclic structure â it only works when there's a cycle
Confusing the meeting point with the cycle start â they are NOT the same (phase 1 vs phase 2)
đź Interview Questions
đ¤ Mock Interview
Practice a live interview for Fast & Slow Pointers (Floyd's Cycle Detection)