Merge Sorted Array
Easy · two pointers
You are given two lists of integers, a and b. Each one is already sorted in ascending order, and either may be empty.
Return a new list that contains every number from both lists, in ascending order. If a value appears several times across the two lists, it appears that many times in the result.
The input lists are not changed.
Examples
Example 1
Input: a = [1, 4, 9], b = [2, 3, 10]
Output: [1, 2, 3, 4, 9, 10]
Taking the smaller front number each time gives 1, 2, 3, 4, 9, 10.
Example 2
Input: a = [], b = [5]
Output: [5]
One list is empty, so the result is just the other one.
Example 3
Input: a = [2, 2], b = [2]
Output: [2, 2, 2]
Equal values are all kept.
Constraints
- 0 <= len(a) <= 2000
- 0 <= len(b) <= 2000
- -10^4 <= a[i], b[i] <= 10^4
- a and b are each sorted in ascending order.