Linked ListBeginner
Fast & Slow Pointers
Use two pointers moving at different speeds to detect cycles, find midpoints, or determine list properties.
Problem: Linked List Cycle Detection
Detect if a linked list [3, 2, 0, -4] has a cycle using fast and slow pointers.
3
2
0
-4
SlowFastMeeting Point
Initialize: slow = head, fast = head
Both pointers start at the head (node 3). Slow moves 1 step, fast moves 2 steps.
Slow Pointer
3(index 0)
Fast Pointer
3(index 0)
Step 1/5
Speed:
When to Use
- Cycle detection in linked list
- Find middle of linked list
- Find start of cycle
- Happy number problem
Key Indicators
- Linked list cycle
- Find middle element
- Detect loop
- Two different speeds
Complexity
Time
O(n)
Space
O(1)
Common Problems
Linked List CycleMiddle of Linked ListHappy NumberFind the Duplicate Number
Code
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True # cycle found
# slow is at the middleWant all 29 patterns with detailed visual walkthroughs?
The complete book includes 84 practice problems, full decision tree, and illustrated step-by-step solutions.
Book Coming Soon ($4.99)