Merge Two Sorted Lists
You are given two lists of integers, first and second, each sorted from smallest to largest. Return one list that holds every number of both, also sorted from smallest to largest. A number that appears in both lists appears in the answer as many times as in both together.
Examples
first = [1, 4, 7], second = [2, 3, 9] → [1, 2, 3, 4, 7, 9]
first = [], second = [5, 6] → [5, 6]
first = [1, 2, 2], second = [2, 3] → [1, 2, 2, 2, 3]
first = [-5, 0], second = [-10, 10] → [-10, -5, 0, 10]Constraints
- Each list holds between 0 and 10,000 numbers, sorted, each between -1,000,000,000 and 1,000,000,000.
- Aim for O(n + m): each number is placed once. Sorting the two lists joined together, at O((n + m) log(n + m)), passes too.
Console
›Run to see what your code prints
Run checks the 4 visible cases here, in your browser. Submit runs all 8 on the judge, 4 of them hidden, and measures how your solution scales.
Expected is the reference solution's answer, from the judge. An input that breaks the statement's other rules may get any answer.