Linked List Cycle

Determine if a linked list has a cycle.

Pattern

Fast/Slow Pointers

This problem follows the Fast/Slow Pointers pattern, commonly found in the Linked List category. Recognizing this pattern is key to solving it efficiently in an interview setting.

Approach

How to Solve It

Slow moves 1 step, fast moves 2 steps. If they meet, there is a cycle.

Key Insight

If there's a cycle, fast will 'lap' slow — the gap decreases by 1 each step, so they must meet within one loop of the cycle.

Step-by-step

  1. 1Initialize slow and fast pointers at the head
  2. 2Move slow one step, fast two steps
  3. 3If they meet, there is a cycle
  4. 4If fast reaches null, there is no cycle

Pseudocode

slow = fast = head
while fast and fast.next:
    slow = slow.next
    fast = fast.next.next
    if slow == fast:
        return true
return false
Complexity Analysis

Time Complexity

O(n)

Space Complexity

O(1)
More Linked List Problems

Master this pattern with YeetCode

Practice Linked List Cycle and similar Linked List problems with flashcards. Build pattern recognition through active recall.

Practice this problem