Two singly linked lists a and b may *merge*: from some node onward they share the exact same tail. Find the value at the first shared node, or report that they never merge.
Because nodes can't be shared across two separately-built lists here, the merge is described structurally. You're given:
- a — list A's values, in order.
- b — list B's values, in order.
- skipA — the 0-based index in a where the shared tail begins, or -1 if the lists don't merge.
- skipB — the 0-based index in b where that same shared tail begins, or -1.
When they merge, a.slice(skipA) is identical to b.slice(skipB) (the shared suffix). Return the value at that first shared node — i.e. a[skipA]. If the lists don't merge, return null.
This is the classic "find the intersection node" interview problem in an array-encoded form; the optimal idea (align the two walkers by the length difference, then advance together) is the same.
Example cases
- merge in the middlein a = [4,1,8,4,5], b = [5,6,1,8,4,5], skipA = 2, skipB = 3out 8a[2..] = [8,4,5] equals b[3..] = [8,4,5]; the first shared value is 8.
- no intersectionin a = [2,6,4], b = [1,5], skipA = -1, skipB = -1out nullThe lists never merge.
- merge at the very start of Ain a = [1,9,1,2,4], b = [3,2,4], skipA = 2, skipB = 1out 1a[2..] = [1,2,4] equals b[1..] = [1,2,4]; first shared value is 1.
Constraints
- The number of nodes in each list is in the range [0, 30000].
- 1 <= Node.val <= 100000
- skipA and skipB are valid indices into their lists, or both -1 when there is no intersection.
- When skipA, skipB >= 0, a.slice(skipA) deep-equals b.slice(skipB).
[4,1,8,4,5]
[5,6,1,8,4,5]
2
3