Two PointersTwo Pointers
Fast & Slow Pointers (Floyd's Cycle Detection)
Advance one pointer twice as fast as another through a linked structure to find cycles, cycle starts, midpoints, and k-th-from-end nodes in O(1) space.
headslowfast
1
•
2
•
3
•
4
•
5
•
6
•
7
•
1/10The tail links back to index 3, forming a cycle — but the algorithm does not know that. Start slow and fast at head; fast moves two nodes per step, slow one.
slow (1 step)fast (2 steps)Meeting point / bothCycle start
PseudocodeLearn Fast & Slow Pointers →
1slow = fast = head2while fast and fast.next:3 slow = slow.next; fast = fast.next.next4 if slow == fast: break # cycle found5if no meeting: return no cycle6p = head; while p != slow: p = p.next; slow = slow.next7return p # cycle startVariables
step0
Complexity
best O(n)
avg O(n)
worst O(n)
space O(1)
Speed