dsa · easy
Linked List Cycle II (start)
You are given the head of a singly linked list that may contain a cycle. Return the **node** where the cycle begins, or null if there is none. The judge prints that node's 0-based index, or -1.
Arguments
head— aListNode(val,next);Noneif the list is empty
Only head. Return the node, not an index. Walk .next.
Example
3 → 2 → 0 → -4 with the tail linked back to 2 → return that node (judge prints 1). No cycle → -1.
Samples print **node values**, then how the judge wired the tail (an index, or -1 for no cycle). That second number is **not** a parameter.
Constraints
0 <= number of nodes <= 4*10^4 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [3,2,0,-4] 1 Expected: 1
Example 2
Input: [1,2] -1 Expected: -1