Merge two simple sorted lists
Input
merge_sorted([1, 3, 5], [2, 4])
Output
[1, 2, 3, 4, 5]
Interleave elements from both lists by comparing current elements. Resulting list stays sorted.
Full lesson preview
Combine two sorted lists into one sorted list in linear time.
Problem statement
Task
Examples
Input
merge_sorted([1, 3, 5], [2, 4])
Output
[1, 2, 3, 4, 5]
Interleave elements from both lists by comparing current elements. Resulting list stays sorted.
Input format
Output format
Constraints
Samples
Input
merge_sorted([1,2],[3,4])
Output
[1, 2, 3, 4]
Concatenate in sorted order by comparing heads of each input list.