Combine Two Ascending Sequences Into One
Linked ListsAuto-graded · 24 test casesLast updated
Tests a two-pointer merge of two non-decreasing integer lists into a single sorted list, handling empty inputs and duplicate values correctly.
You are given two integer lists, first and second. Each one is already sorted in non-decreasing order (equal neighbors are allowed). Produce a single list that contains every element from both inputs, also in non-decreasing order.
This is the same operation you would perform when stitching two sorted linked lists together, here expressed on Python lists.
Requirements
- The result must include all elements of
firstand all elements ofsecond, with duplicates preserved (nothing is removed or merged). - The result must be sorted in non-decreasing order.
- Either input may be empty. If both are empty, return an empty list.
- Do not modify the input lists.
- Aim for a single pass over both inputs rather than concatenating and re-sorting.
Constraints
0 <= len(first), len(second)- Both
firstandsecondare sorted in non-decreasing order. - Elements are integers (negative values and zero are allowed).
- The return value is a new list of length
len(first) + len(second).
Examples
Example 1
Input: merge_two_sorted([1, 2, 4], [1, 3, 4])
Output: [1, 1, 2, 3, 4, 4]
Example 2
Input: merge_two_sorted([], [0])
Output: [0]
Example 3
Input: merge_two_sorted([], [])
Output: []
Example 4
Input: merge_two_sorted([1, 2, 5, 8], [2, 2, 6])
Output: [1, 2, 2, 2, 5, 6, 8]
Hints
Hint 1
Keep one index into each list. At every step, look only at the two elements currently pointed to.
Hint 2
Append the smaller of the two current elements to the output and advance only that list's index. When values are equal, taking from either list gives the same result.
Hint 3
When one list runs out, the rest of the other list is already sorted, so you can append it in one go.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Use two pointers, i into first and j into second, and build the output step by step.
- While both pointers are within their lists, compare
first[i]andsecond[j]. Append the smaller one to the result and advance the pointer of the list it came from. On a tie, take fromfirst; either choice produces the same values. - When one list is exhausted, the remaining elements of the other list are already in sorted order and are all at least as large as everything appended so far. Append that leftover suffix directly.
- Return the result.
Why it is correct
At every step, the smallest element not yet placed must be either first[i] or second[j], because each list is sorted and the pointers sit at the smallest unused element of each. Choosing the smaller of the two therefore always extends the output with the next correct value. Once one list is empty, the other list's remainder is sorted and bounded below by what has been output, so appending it completes a sorted result. Duplicates are kept because every element is appended exactly once.
Complexity
- Time: O(m + n), where m and n are the lengths of the inputs; each element is handled once.
- Space: O(m + n) for the output list (O(1) extra beyond the output).
Reference solution
from typing import List
def merge_two_sorted(first: List[int], second: List[int]) -> List[int]:
i = j = 0
result = []
while i < len(first) and j < len(second):
if first[i] <= second[j]:
result.append(first[i])
i += 1
else:
result.append(second[j])
j += 1
result.extend(first[i:])
result.extend(second[j:])
return result