Intersect sorted lists
Return the sorted multiset intersection of two sorted integer lists. Retain each value the smaller number of times it occurs.
a, b = [1, 2, 2], [2, 2, 3]
i = j = 0
print(a[i] < b[j]) # advance only iReason about the state
Advance the pointer holding the smaller value. Equal values consume one item from both inputs, yielding linear total work. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Return the sorted multiset intersection of two sorted integer lists. Retain each value the smaller number of times it occurs.
The explanation opens here once every check passes.