Merge Two Sorted Lists
Merge Two Sorted Lists
Imagine you have two lists of numbers. Each list is already sorted in increasing order. Your task is to merge these two lists into a single list that is also sorted in increasing order.
How does it work?
You will be given two sorted lists, and you need to return a new list that contains all the elements from both lists in sorted order. This problem will help you understand how to work with lists and manage sorted data in Python.
Why is this fun?
By solving this challenge, you'll learn how to manipulate lists, which are one of the most useful data types in Python. Plus, you'll get a sense of accomplishment by writing a function that can handle lists of any length!
For example, if you have:
list1 = [1, 3, 5] list2 = [2, 4, 6]
Your function should merge these lists into:
[1, 2, 3, 4, 5, 6]
Examples:
merge_sorted_lists([1, 3, 5], [2, 4, 6])should return[1, 2, 3, 4, 5, 6]merge_sorted_lists([1, 2, 2], [2, 3, 4])should return[1, 2, 2, 2, 3, 4]merge_sorted_lists([], [1, 2, 3])should return[1, 2, 3]merge_sorted_lists([4, 6, 8], [])should return[4, 6, 8]
Now it's your turn to write the function!
Inputs
Arg 1:
[1,3,5]Arg 2:
[2,4,6]Expected Output
[1, 2, 3, 4, 5, 6]Actual Output