You are given the heads of two sorted linked lists list1
and list2
.
Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.
Return the head of the merged linked list.
Example 1:
Input: list1 = [1,2,4], list2 = [1,3,4] Output: [1,1,2,3,4,4]
Example 2:
Input: list1 = [], list2 = [] Output: []
Example 3:
Input: list1 = [], list2 = [0] Output: [0]
Constraints:
[0, 50]
.-100 <= Node.val <= 100
list1
and list2
are sorted in non-decreasing order.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force way to combine two sorted lists is to simply create a new, third list and put all elements from both lists into it. After that, we sort the combined list to achieve the final merged, sorted order. It is like mixing two decks of sorted playing cards, and then re-sorting the whole deck.
Here's how the algorithm would work step-by-step:
def merge_two_sorted_lists_brute_force(list1, list2):
# Create a new list to hold all elements
merged_list = []
# Add all elements from the first list
for element_from_list1 in list1:
merged_list.append(element_from_list1)
# Add all elements from the second list
for element_from_list2 in list2:
merged_list.append(element_from_list2)
# Sort the merged list to ensure correct order
merged_list.sort()
return merged_list
The clever way to merge two sorted lists is to build a new sorted list piece by piece, always picking the smallest element available. Instead of combining the lists and then sorting, we directly create a sorted result. This saves time by avoiding a full sort at the end.
Here's how the algorithm would work step-by-step:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def mergeTwoLists(list1, list2):
# Handle the case where either list is empty.
if not list1:
return list2
if not list2:
return list1
# Initialize the head of the merged list.
if list1.val <= list2.val:
merged_head = list1
list1 = list1.next
else:
merged_head = list2
list2 = list2.next
current_node = merged_head
# Iterate while both lists have elements.
while list1 and list2:
# Choose the smaller value from the two lists.
if list1.val <= list2.val:
current_node.next = list1
list1 = list1.next
else:
current_node.next = list2
list2 = list2.next
current_node = current_node.next
# Append the remaining elements from list1, if any.
if list1:
current_node.next = list1
# Append the remaining elements from list2, if any.
if list2:
current_node.next = list2
return merged_head
Case | How to Handle |
---|---|
Both input lists are null | Return null since there's nothing to merge. |
One list is null, the other is not | Return the non-null list directly as it is already sorted. |
Both lists are empty (have no nodes) | Return null since the result should be an empty list. |
One list is empty, the other has one or more nodes | Return the non-empty list. |
Lists contain duplicate values | The standard merging logic handles duplicates correctly, preserving their order. |
Lists contain negative values, zeros, and positive values | The comparison based merging logic works correctly regardless of the sign of the values. |
One list has significantly fewer nodes than the other | The algorithm will iterate through the shorter list and then append the remainder of the longer list. |
Lists are identical (contain the same values in the same order) | The merging process will interleave the identical values, maintaining the sorted order. |