Skip to interview prep
Apexed

14. Detect a Cycle in a Linked List

EasyLinked lists

Return true if the linked list contains a cycle. Use O(1) extra memory.

The tests write the list as { values, pos }: the tail links to the node at index pos, or to nothing when pos is -1.

Example 1

Input: values = [3, 2, 0, -4], pos = 1
Output: true

Constraints

  • 0 ≤ nodes ≤ 10⁴

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 5 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Detect a Cycle in a Linked List — coding interview practice — Apexed