‹ DS&A interview · Socratic
DSA · Linked list / two pointer · #04

Linked List Cycle II

Module 6 · difficulty 3/5·30:00starts on first keystroke

Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return `null`. A cycle exists if some node in the list can be reached again by continuously following the `next` pointer. The list is described by an index `pos` which points to the tail's connection target (or -1 if no cycle); `pos` is not passed to the function. Do not modify the linked list.

Examples
  • head = [3,2,0,-4], pos = 1 node at index 1 (value 2)tail connects to index 1
  • head = [1,2], pos = 0 node at index 0 (value 1)
  • head = [1], pos = -1 nullno cycle
Constraints
  • · Number of nodes in [0, 10^4].
  • · -10^5 <= Node.val <= 10^5
  • · pos is -1 or a valid index in the linked list.
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.