Write a function that takes two lists, each of which is sorted in increasing order, and merges the two into a single list in increasing order, and returns it.

For example, consider lists a = {1, 3, 5, 7} and b = {2, 4, 6}. Merging them should yield the list {1, 2, 3, 4, 5, 6, 7}.

Practice this problem

The problem can be solved either iteratively or recursively. There are many cases to deal with – either a or b may be empty during processing, either a or b can run out first, and finally, there’s the problem of starting the result list empty, and building it up while going through a and b.

1. Using Dummy Nodes

The strategy here uses a temporary dummy node as the start of the result list. The pointer tail always points to the last node in the result list, so appending new nodes is easy. The dummy node gives the tail something to point to initially when the result list is empty. This dummy node is efficient since it is only temporary, and it is allocated in the stack. The loop proceeds, removing one node from either a or b and adding it at the tail. When we are done, the result is in dummy.next.

Following is the C, Java, and Python implementation of the idea:

C


Download  Run Code

Output:

First List: 1 —> 3 —> 5 —> 7 —> NULL
Second List: 2 —> 4 —> 6 —> NULL
After Merge: 1 —> 2 —> 3 —> 4 —> 5 —> 6 —> 7 —> NULL

Java


Download  Run Code

Output:

First List: 1 —> 3 —> 5 —> 7 —> null
Second List: 2 —> 4 —> 6 —> null
After Merge: 1 —> 2 —> 3 —> 4 —> 5 —> 6 —> 7 —> null

Python


Download  Run Code

Output:

First List: 1 —> 3 —> 5 —> 7 —> None
Second List: 2 —> 4 —> 6 —> None
After Merge: 1 —> 2 —> 3 —> 4 —> 5 —> 6 —> 7 —> None

2. Using Local References

This solution is structurally very similar to the above, but it avoids using a dummy node. Instead, it maintains a struct node** pointer, lastPtrRef, which always points to the last pointer of the result list. This solves the same case that the dummy node did – dealing with the result list when it is empty. When trying to build up a list at its tail, use either the dummy node or the struct node** “reference” strategy.

This approach is demonstrated below in C:

C


Download  Run Code

Output:

First List: 1 —> 3 —> 5 —> 7 —> NULL
Second List: 2 —> 4 —> 6 —> NULL
After Merge: 1 —> 2 —> 3 —> 4 —> 5 —> 6 —> 7 —> NULL

3. Using Recursion

This is a nice problem where the recursive solution code is much cleaner than the iterative code. The recursive implementation can be seen below in C, Java, and Python:

C


Download  Run Code

Output:

First List: 1 —> 3 —> 5 —> 7 —> NULL
Second List: 2 —> 4 —> 6 —> NULL
After Merge: 1 —> 2 —> 3 —> 4 —> 5 —> 6 —> 7 —> NULL

Java


Download  Run Code

Output:

First List: 1 —> 3 —> 5 —> 7 —> null
Second List: 2 —> 4 —> 6 —> null
After Merge: 1 —> 2 —> 3 —> 4 —> 5 —> 6 —> 7 —> null

Python


Download  Run Code

Output:

First List: 1 —> 3 —> 5 —> 7 —> None
Second List: 2 —> 4 —> 6 —> None
After Merge: 1 —> 2 —> 3 —> 4 —> 5 —> 6 —> 7 —> None

The time complexity of all above-discussed methods is O(m + n), where m and n are the total number of nodes in the first and second list, respectively. The iterative version doesn’t require any extra space, whereas the recursive version uses stack space proportional to the lists’ length.

 
Source: http://cslibrary.stanford.edu/105/LinkedListProblems.pdf