Given two linked lists, merge their nodes to make one list, taking nodes alternately between the two lists. If either list runs out, all the nodes should be taken from the other list.

For example, merging lists {1, 2, 3} and {7, 13, 1} should yield {1, 7, 2, 13, 3, 1}.

Practice this problem

The solution depends on being able to move nodes to the end of the list. Several techniques are available to solve this problem: dummy node, local reference, or recursion.

1. Using Dummy Node

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 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 to 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 Dummy Node with moveNode() function

This method is logically the same as above, but it uses the moveNode() function as a helper. This approach is demonstrated below in C:

C


Download  Run Code

3. Using Local References

This solution is structurally very similar to the above, but avoids using a dummy node. Instead, it maintains a struct node** pointer, lastPtrRef, which always points to the last node 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, we can use either the dummy node or the struct node** “reference” strategy.

Following is the implementation in C based on the above 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

4. Recursive

The recursive solution is the most compact of all but is probably not appropriate for production code since it uses stack space proportionate to the lists’ lengths.

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.

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