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 <= 100list1 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 strategy involves combining all the items from both lists into one big, unsorted collection first. Then, it sorts this combined collection from scratch to get the final merged and sorted list.
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 merge_two_lists_brute_force(first_list_head, second_list_head):
all_values = []
current_node = first_list_head
# First, collect all values from the first list into a single collection.
while current_node is not None:
all_values.append(current_node.val)
current_node = current_node.next
current_node = second_list_head
# Next, collect all values from the second list into the same collection.
while current_node is not None:
all_values.append(current_node.val)
current_node = current_node.next
# Sort the combined collection of values to establish the final merged order.
all_values.sort()
dummy_head = ListNode()
current_new_node = dummy_head
# Finally, construct a new linked list from the sorted values.
for value in all_values:
current_new_node.next = ListNode(value)
current_new_node = current_new_node.next
return dummy_head.nextThe most efficient way to solve this is by building the new sorted list one piece at a time. We repeatedly look at the smallest available items from both original lists and pick the smaller of the two to add to our new list, advancing only the list we picked from.
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(list_node1, list_node2):
# Create a starting point for the new merged list, which helps manage the head of the new list.
dummy_head = ListNode()
current_merged_list_pointer = dummy_head
while list_node1 and list_node2:
# Compare nodes from both lists to decide which one to add next to maintain sorted order.
if list_node1.val < list_node2.val:
current_merged_list_pointer.next = list_node1
list_node1 = list_node1.next
else:
current_merged_list_pointer.next = list_node2
list_node2 = list_node2.next
current_merged_list_pointer = current_merged_list_pointer.next
# If one list is exhausted, the other must contain all remaining (and larger) elements.
if list_node1:
current_merged_list_pointer.next = list_node1
elif list_node2:
current_merged_list_pointer.next = list_node2
# The merged list starts right after the dummy node we created.
return dummy_head.next| Case | How to Handle |
|---|---|
| One or both input lists are null or empty | The algorithm should return the non-null list, or null if both are empty. |
| One list is exhausted while the other still has elements | The remaining portion of the non-empty list is appended to the end of the merged list. |
| Both lists contain only a single node | The smaller node becomes the head, and its next pointer is set to the larger node. |
| Lists contain duplicate values across or within them | The relative order of duplicate values is preserved by the stable merging logic. |
| Lists contain negative numbers and zeros | The standard comparison logic correctly handles negative numbers, zeros, and positive numbers. |
| One list is significantly longer than the other | The solution efficiently appends the long remaining tail after the shorter list is fully merged. |
| All elements in one list are smaller than all elements in the other | The algorithm will append the second list entirely to the end of the first list. |
| Lists have nodes with extreme values (e.g., Integer.MIN_VALUE, Integer.MAX_VALUE) | Standard integer comparisons will handle these boundary values correctly without overflow issues. |