Given the starting nodes of two singly linked lists, headA and headB, identify and return the node both lists first share. Return null when the lists never join.
For readability, each example's output displays the value stored in the returned node; the required return remains the node reference.
Test cases are represented as [listA, listB, skipA, skipB]. The values skipA and skipB specify the number of nodes preceding the shared portion in their respective lists. From those positions onward, the harness makes both lists point to the exact same tail. Determine an intersection by node reference rather than by comparing node values.
Example 1:
Input: listA = [7,3,9,2], listB = [6,9,2], skipA = 2, skipB = 1
Output: 9 (the returned node's value)
Explanation:
Both lists connect to the tail [9,2]. The returned node is the first shared node, which stores 9.
listA = [7,3, 9, 2] listB = [6, 9, 2] skipA = 2 skipB = 1
9
We start with two singly linked lists. List A (top) has nodes 7 → 3 → 9 → 2. List B (bottom) has nodes 6 → 9 → 2.
Example 2:
Input: listA = [8,1,5,7], listB = [4,6,7], skipA = 3, skipB = 2
Output: 7
Explanation:
The lists meet at their shared tail [7], so the intersection node's value is 7.
Example 3:
Input: listA = [3,8,1], listB = [9,4], skipA = -1, skipB = -1
Output: null
Explanation:
These lists do not share any node.
0 <= listA.length, listB.length <= 3 * 10^41 <= Node.val <= 10^50 <= skipA <= listA.length or skipA == -10 <= skipB <= listB.length or skipB == -1listA = [7,3, 9, 2] listB = [6, 9, 2] skipA = 2 skipB = 1
9
We start with two singly linked lists. List A (top) has nodes 7 → 3 → 9 → 2. List B (bottom) has nodes 6 → 9 → 2.