FunctionRacerProblemsRace

Zip Two Sorted Lists

BeginnerTwo pointersPython, JavaScript, TypeScript, Java, C++

a and b are both sorted from smallest to largest. Return one sorted list that holds every item of both lists, repeated values included.

Sorting the joined lists would work, but the order they already have allows a faster way: keep one position in each list, and always take the smaller of the two items you are looking at.

Example 1

a [1,4,6]
b [2,3,7]
returns [1,2,3,4,6,7]

Taking the smaller front item each time gives 1, 2, 3, 4, 6, 7.

Example 2

a [1,2]
b []
returns [1,2]

b is empty, so the answer is a.

Example 3

a [5]
b [5]
returns [5,5]

Both 5s belong in the answer.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Two pointers problems