Skip to content

Floyd's Cycle Detection

Task: Find out whether this list loops, using two pointers

head1234567fastslowslow === fast

What you are watching

Floyd's cycle detection, run on a seven node list whose last node points back to the fourth. Two pointers start at the head; the slow one advances one node per round and the fast one advances two. Round by round the gap closes until both land on the same node, which is the proof that the list contains a loop. A list that simply ended would instead run the fast pointer off the end.