#141

Linked List Cycle

Easy Floyd's cycle detection

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Decide whether a linked list has a cycle (some node's next points back to an earlier node).

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: does the list loop back on itself?

Why this works: on a circular track a faster runner always laps a slower one; on a straight road the fast one reaches the end.

Example: 3 → 2 → 0 → -4 → (back to 2)

slow 2, fast 0 → slow 0, fast 2 → slow -4, fast -4 → meet → cycle

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Checking slow == fast before moving them — they start equal, so you'd always say "cycle".

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(1).
Topic: Linked List
🎬 Episodes 40–45: video coming soon