Dashboard
LC #141

Linked List Cycle

Pattern · ?AmazonGoogleMicrosoftApple

Problem

Detect if a linked list has a cycle, using O(1) memory.

Constraints

  • 0 ≤ n ≤ 10⁴
  • -10⁵ ≤ Node.val ≤ 10⁵
  • pos = -1 means no cycle; otherwise pos is the index of the node the tail connects to
  • Must use O(1) memory

Example

Input
head = [3, 2, 0, -4], pos = 1
Output
true
Why
Tail node (-4) points back to node at index 1 (value 2), forming a cycle

Spot the pattern

Which pattern cracks it?

Hints

One at a time
Solve on LeetCode ↗