easy

Linked List Cycle

Given the head of a singly linked list, determine whether following next pointers ever revisits a node (i.e. the list contains a cycle). Use constant extra memory.

Constraints
  • 0 ≤ number of nodes ≤ 10^4
  • O(1) space required
Examples
in: head = 3 → 2 → 0 → -4 → (back to 2)
out: true
in: head = 1 → 2
out: false

Code it yourself

Solve in
Test execution is not yet available for this exercise.Practice journal →Draft saved in this browser.
Hints:
Which approach applies?

Choose an approach to check your pattern recognition, or reveal the discussion when you need help.