Solution: Merge Two Sorted Lists
Let's solve the Merge Two Sorted Lists problem using the K-way Merge pattern.
We'll cover the following
Statement#
Given the heads of two sorted linked lists, list1 and list2, merge these lists into a single sorted list. This involves integrating all the nodes from both lists while ensuring that their sorted order is preserved. Return the head of the merged linked list as the output.
Constraints:
- Number of nodes in both lists 
- Node.data
- Both lists are sorted in a non-decreasing order. 
Solution#
The objective is to combine the given linked lists into a single, sorted linked list while maintaining their inherent order. This can be accomplished through a systematic approach where we traverse both lists simultaneously and compare the values of the current nodes in both lists. The smaller value is chosen, and the respective node becomes part of the merged list. This process continues iteratively, and eventually, the merged list contains all the nodes of list1 and list2 in a non-decreasing order. We can then return this list as the correct output.
To store the output, a new linked list is created with its first node, dummy, initialized to prev, is initialized to point to the dummy node. This will be used to keep track of the last node in the merged list. Next, a loop is started, which continues as long as both head1 and head2 are not None. In each iteration:
- A comparison is made between the data of the current nodes pointed to by - head1and- head2.- If the value pointed to by - head1is less than or equal to the value pointed to by- head2, it means the next node in the merged list should come from- head1. Therefore,- prev.nextis set to- head1and then- head1moves to the next node in its list.
- Otherwise, the next node in the merged list should come from - head2. Therefore,- prev.nextis set to- head2, and- head2moves to the next node in its list.
 
- Regardless of which node has been added to the merged list, - previs updated to point to this recently added node.
The loop ends if either head1 or head2 becomes None. Now, once we are outside the loop, if there are any remaining nodes in either list1 or list2, we append all of them to the merged list at once.
- If - head1is not- None, it means there are nodes remaining in- list1, so the rest of- list1is directly appended to the merged list using- prev.next- head1.
- Otherwise, there are nodes remaining in - list2, so the rest of- list2is appended to the merged list using- prev.next- head2.
By this time, we will have all the nodes present in list1 and list2 integrated into the new linked list in a non-decreasing order. We'll return the node next to the dummy node of this merged list, i.e., dummy.next.
Let’s look at the following illustration to get a better understanding of the solution:
1 of 14
2 of 14
3 of 14
4 of 14
5 of 14
6 of 14
7 of 14
8 of 14
9 of 14
10 of 14
11 of 14
12 of 14
13 of 14
14 of 14
Let’s look at the code for the solution below:
Time complexity#
The while loop iterates as long as both list1 and list2 have nodes left to be merged. In the worst case, it will run for the total number of nodes in both lists, which is list1 and list2. The operations inside the loop and outside the loop take constant time. Therefore, the overall time complexity of this solution is 
Space complexity#
The space complexity of this solution is 
Merge Two Sorted Lists
Merge K Sorted Lists