LeetCopilot Logo
LeetCopilot
Home/Blog/Linked List Cycle Detection Dry Run for Beginners

Linked List Cycle Detection Dry Run for Beginners

LeetCopilot Team
Apr 2, 2025
10 min read
Linked listsTwo pointersInterview prepDebuggingCycles
A gentle, step-by-step guide to detecting cycles with Floyd's tortoise and hare, including drawings, code, and interview narration tips.

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.

TL;DR

  • Floyd's cycle detection uses a slow pointer (1 step) and fast pointer (2 steps) to spot cycles.
  • It's interview-friendly because it runs in O(n) time and O(1) space without modifying the list.
  • Core loop: move slow once, fast twice, check equality. If they meet, cycle exists; otherwise fast hits null.
  • Beginners often misplace the null checks or forget to reset pointers when finding the cycle entry.
  • You'll learn a dry-run pattern, a simple code sample, pitfalls, and practice drills that fit LeetCode-style prompts.

Beginner-Friendly Explanations

Why Two Pointers Work

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.

What the Algorithm Guarantees

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.

When to Choose This Approach

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.

Step-by-Step Learning Guidance

1) Draw the List With Indices

Sketch nodes as boxes with arrows. Mark the cycle connection explicitly. Label head, slow, and fast so you can narrate pointer moves.

2) Run the Detection Loop

At each iteration: slow = slow.next; fast = fast.next?.next. Stop if fast is null (no cycle) or slow === fast (cycle found).

3) Find the Entry (Optional but Popular)

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.

4) Verify With a Tiny Example

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.

5) Translate to Code With Checks First

Write null guards before moving fast twice. Doing so prevents runtime errors and shows disciplined reasoning, a habit emphasized on the learning tools page.

Visualizable Example: Floyd's Cycle Detection in TypeScript

ts
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.

Practical Preparation Strategies

Narrate Every Move

Say "slow moves one, fast moves two" aloud. This keeps you from skipping the null check mid-interview.

Alternate Implementations

Rehearse the hash-set variant once, then contrast space usage with Floyd's. The comparison proves you considered trade-offs.

Time-Boxed Drills

Give yourself two minutes to sketch and explain a dry run. Repeat with different cycle positions to avoid overfitting to one case.

Use Supportive Hints

When stuck, a tool like LeetCopilot can highlight whether your fast pointer advances safely without overriding your own reasoning.

Common Mistakes to Avoid

Moving Fast Before Checking Null

Always confirm fast and fast.next are non-null before fast.next.next.

Forgetting the Second Phase for Entry

If the problem asks for the cycle start, you must reset a pointer and move both at speed 1.

Mutating the List

Do not break pointers to test for cycles unless explicitly allowed; it can corrupt inputs in shared environments.

Assuming Meeting Means Entry

The first meeting point is not guaranteed to be the entry. Run the second phase to be precise.

FAQ

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.

Conclusion

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.

Want to Practice LeetCode Smarter?

LeetCopilot is a free browser extension that enhances your LeetCode practice with AI-powered hints, personalized study notes, and realistic mock interviews — all designed to accelerate your coding interview preparation.

Also compatible with Edge, Brave, and Opera

Related Articles