 All Problems
Linked List Cycle II
medium
linked list
two pointers
hash table
amazon
google
microsoft
facebook

Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null.

Do not modify the linked list.

Example 1:

Input:
3 2 0 -4
1
Output: 2

(Cycle starts at node with value 2, index 1)

Example 2:

Input:
1 2
0
Output: 1

Example 3:

Input:
1
-1
Output: null

Constraints:

  • The number of nodes is in the range [0, 10⁴]
  • pos is -1 or a valid index

Input format: First line: node values. Second line: pos (-1 = no cycle).

Output format: Cycle start node value, or null.

Run to check your code against the sample cases, or submit to run every case