Editor

Merge Two Sorted Lists

#44 · Two pointers · Beginner · 40 XP · about 8 min

Two lists of numbers are each sorted in ascending order. Write merge_sorted(a, b) that returns one new list holding every number from both, in ascending order. Keep duplicates.

Try it without sort() or sorted(): both lists are already in order, and that is enough to build the result in a single walk.

Examples

merge_sorted([1, 4, 9], [2, 3, 10])   # returns [1, 2, 3, 4, 9, 10]
merge_sorted([5, 5], [5])             # returns [5, 5, 5]
merge_sorted([], [1, 2])              # returns [1, 2]

Constraints

  • 0 <= len(a), len(b) <= 10,000
  • Both lists are sorted in ascending order

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.