Fast/Slow Pointers (Linked List)
Move two linked-list references at different speeds to detect cycles or locate a middle position.
The task asks about a cycle, midpoint, kth-from-end node, or a linked-list position without knowing the length first.
How to approach it
Use a null check before fast advances twice. For kth-from-end, keep a lead pointer k nodes ahead, then move both together.
Define behavior for empty lists, one-node lists, and whether the requested position is counted from zero or one.
Practice question
A linked list has 7 nodes. Using slow-by-one and fast-by-two, which node does slow reach when fast reaches the end?
List: A → B → C → D → E → F → G → nullGive your answer before revealing the explanation.Show a hint
Each fast-pointer move covers two nodes while slow advances one.
Show an answer
D, the middle node.
Fast/Slow Pointers (Linked List) at work
A music playlist stores each track's next-track link. The last track accidentally links back to track 3. How can you detect the loop without storing every track ID?
Show one possible approach
Start slow and fast at the first track. Advance slow by one link and fast by two; if they meet, a cycle exists. This uses constant extra space.
Practice next
Use these problem IDs to find additional practice for this pattern: