All problems
Intersection Point of Two Lists
Problem
Find where two singly linked lists merge.
a and b hold the node values, and a[skip_a:] and b[skip_b:] are the shared tail. Align the pointers by advancing the pointer in the longer tail by the length difference, then walk forward until the values first match.
Return the index in b where the shared tail begins, or -1 when there is no intersection.
Hint
Skip the length difference between the tails first so both pointers reach the shared part together.
Test cases
case 1
in: [[4,1,8,4,5],[5,6,1,8,4,5],2,3]
expected: 3
case 2
in: [[1,9,1,2,4],[3,2,4],3,1]
expected: 1
case 3
in: [[2,6,4],[1,5],3,2]
expected: -1
case 4
in: [[1],[1],0,0]
expected: 0
Starter codePython
def list_intersection(a, b, skip_a, skip_b):
# Your code here
pass4 test cases · run your code against them in the editor