Merge Two Sorted Lists
Write merge_lists(l1, l2) that merges two sorted lists into one sorted list and returns it as a list. Every element of both inputs must appear in the result (duplicates are kept), and either input may be empty. The tests print the returned list as space-separated values.
Example
1,2,4 1,3,4
1 1 2 3 4 4
- The inputs are two lists,
l1= [1, 2, 4] andl2= [1, 3, 4], each already in non-decreasing order. They can have different lengths, and either one can be empty. - The output holds every element from both lists exactly once, so its length is . Nothing is dropped and nothing is de-duplicated.
- Count each value across the two inputs: appears once in each list, so twice in total. and appear once each, and appears once in each list, so twice. In non-decreasing order that gives .
- The function returns that list, [1, 1, 2, 3, 4, 4]. The tests print it with spaces between the values:
1 1 2 3 4 4. - Notice that when one input is empty, the result is just the other list, and two empty inputs give an empty list (printed as
EMPTY).
Constraints
- 0 <= number of nodes <= 50
- -100 <= Node.val <= 100