Documentation Index
Fetch the complete documentation index at: https://swe.aboneda.com/llms.txt
Use this file to discover all available pages before exploring further.
Fast & Slow Pointers
A two-pointer technique where one pointer moves faster than the other, primarily used for cycle detection and finding middle elements.Floyd’s Cycle Detection
Also known as the tortoise and hare algorithm:- Slow pointer moves 1 step at a time
- Fast pointer moves 2 steps at a time
- If there’s a cycle, they will meet
- If fast reaches the end, there’s no cycle
Finding the Cycle Start
After detecting a cycle (slow and fast meet):- Reset one pointer to the head
- Move both pointers one step at a time
- They meet at the cycle start
Finding the Middle
Use fast/slow to find the middle of a linked list in one pass:Classic Problems
- Linked List Cycle I & II
- Find the Duplicate Number (array as linked list)
- Middle of the Linked List
- Palindrome Linked List (find middle + reverse second half)
- Happy Number (cycle in digit-square sequence)
- Reorder List
How It Differs from Two Pointers
| Aspect | Two Pointers | Fast & Slow |
|---|---|---|
| Speed | Same speed, different directions | Different speeds, same direction |
| Use case | Sorted arrays, pair finding | Cycles, middle finding |
| Data structure | Arrays, strings | Linked lists, sequences |
When to Use
- Detecting cycles in linked lists or sequences
- Finding the middle element in one pass
- Problems where the structure “loops back” (e.g., Happy Number)
- Linked list problems requiring O(1) space
