Skip to content
Decoder

Linked List Cycle II

Medium

Problem

Given the head of a linked list, return the node where the cycle begins: the first node you reach twice when following next from the head. If the list has no cycle, return None (null in TypeScript). Do not change the list, and try to use O(1) extra memory.

Example 1

Input
3
Output
2 -> 0 -> -4

Example 2

     ^          |

Example 3

Input
+----------+
Output
the node holding 2

Example 4

Input
1
Output
2

Example 5

^    |

Example 6

Input
+----+
Output
the node holding 1

Example 7

Input
1
Output
(end) → None

The tests build each list, link its tail back to a chosen node, and check that you return that exact node object.

Constraints

  • 0 <= number of nodes <= 10^4
  • -10^5 <= Node.val <= 10^5
  • If there is a cycle, the tail links back to a node in the list.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.