Alex HunterDon't just memorize 'fast moves 2 steps'. Learn to explain the math and intuition behind Floyd's Cycle Detection in plain English.
Originally published on LeetCopilot Blog
Don't just memorize 'fast moves 2 steps'. Learn to explain the math and intuition behind Floyd's Cycle Detection in plain English.
Cycle detection feels abstract until you see the pointers move. This walkthrough builds intuition with drawings, narrated steps, and a small TypeScript snippet so you can explain Floyd's algorithm calmly in interviews.
If a cycle exists, the fast pointer eventually laps the slow pointer like a runner on a track. Without a cycle, fast falls off the track (reaches null) first.
Meeting implies a cycle exists but not where it starts. A second phase resets one pointer to head to locate the entry—critical to mention in the interview communication guide.
Use it when memory is tight or node mutation is disallowed. For problems allowing extra space, a hash set is simpler but less space-efficient.
Sketch nodes as boxes with arrows. Mark the cycle connection explicitly. Label head, slow, and fast so you can narrate pointer moves.
At each iteration: slow = slow.next; fast = fast.next?.next. Stop if fast is null (no cycle) or slow === fast (cycle found).
Reset one pointer to head. Move both one step at a time; the meeting node is the cycle start. Practice saying this as you would in a mock interview routine.
Use a 5-node list where node 4 points back to node 2. Trace positions after each step and record them in a table to visualize convergence.
Write null guards before moving fast twice. Doing so prevents runtime errors and shows disciplined reasoning, a habit emphasized on the learning tools page.
type ListNode = { val: number; next: ListNode | null };
function hasCycle(head: ListNode | null): boolean {
let slow = head;
let fast = head;
while (fast && fast.next) {
slow = slow!.next;
fast = fast.next.next;
if (slow === fast) return true;
}
return false;
}
Draw slow and fast after each loop. When they overlap, shade that node to show the detection. If fast reaches null, draw an X at the tail to illustrate the no-cycle exit.
Say "slow moves one, fast moves two" aloud. This keeps you from skipping the null check mid-interview.
Rehearse the hash-set variant once, then contrast space usage with Floyd's. The comparison proves you considered trade-offs.
Give yourself two minutes to sketch and explain a dry run. Repeat with different cycle positions to avoid overfitting to one case.
When stuck, a tool like LeetCopilot can highlight whether your fast pointer advances safely without overriding your own reasoning.
Always confirm fast and fast.next are non-null before fast.next.next.
If the problem asks for the cycle start, you must reset a pointer and move both at speed 1.
Do not break pointers to test for cycles unless explicitly allowed; it can corrupt inputs in shared environments.
The first meeting point is not guaranteed to be the entry. Run the second phase to be precise.
How do I know I'm using the pattern correctly?
If your loop condition is while (fast && fast.next) and you check equality after advancing, you're aligned with Floyd's algorithm.
What should I practice before this topic?
Pointer basics, null checks, and simple linked list traversal to build comfort with references.
Is this concept important for interviews?
Yes—cycle detection appears frequently and demonstrates pointer discipline in O(1) space.
How do I explain the entry-finding phase?
After detection, reset one pointer to head and move both one step at a time; the meeting node is the start of the cycle.
A linked list cycle detection dry run for beginners hinges on seeing the pointers move, not memorizing proofs. Draw the track, narrate each step, guard against null, and practice the entry phase so your interview explanations stay calm and confident. Gentle nudges from tools like LeetCopilot can catch skipped checks without replacing your reasoning.
If you're looking for an AI assistant to help you master LeetCode patterns and prepare for coding interviews, check out LeetCopilot.