Merge Two Sorted Lists

EasyTwo Pointers≈ 15 min

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

text
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.
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
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.