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^5If there is a cycle, the tail links back to a node in the list.