easyLinked ListAmazonMicrosoft
Loop Detector
Audit a linked chain for a hidden feedback loop. Report whether any node is reachable from itself.
Given the head of a linked list, decide whether the list contains a cycle. Use O(1) extra space.
Choose approach
3
slowfast
2
0
-4
1/5
Narrationstep 1 of 5
slow + fast both start at head. slow steps 1 node, fast steps 2 nodes. They meet inside a cycle.
Variables
slow=3
fast=3
How to think about it
- In a cycle, the fast pointer gains one node on the slow each iteration.
- After at most n iterations inside the cycle, they coincide.
- No cycle ⇔ fast reaches null without meeting slow.
Edge cases to catch in interview
- Empty list or single node without self-loop → false.
- Self-loop (node points to itself) → fast == slow at first advance.
- Head is inside the cycle too → still detected.
Examples
Example 1
true{"values":[3,2,0,-4],"pos":1}
Tail links back to index 1.
Example 2
false{"values":[1,2],"pos":-1}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed