Python Program To Merge Two Sorted Lists (In-Place)
Given two sorted lists, merge them so as to produce a combined sorted list (without using extra space).
Examples:
Input: head1: 5->7->9
head2: 4->6->8
Output: 4->5->6->7->8->9
Explanation: The output list is in sorted order.
Input: head1: 1->3->5->7
head2: 2->4
Output: 1->2->3->4->5->7
Explanation: The output list is in sorted order.
There are different discussed different solutions in post below.Â
Merge two sorted linked lists
Method 1 (Recursive):
Approach: The recursive solution can be formed, given the linked lists are sorted.
- Compare the head of both linked lists.
- Find the smaller node among the two head nodes. The current element will be the smaller node among two head nodes.
- The rest elements of both lists will appear after that.
- Now run a recursive function with parameters, the next node of the smaller element, and the other head.
- The recursive function will return the next smaller element linked with rest of the sorted element. Now point the next of current element to that, i.e curr_ele->next=recursivefunction()
- Handle some corner cases.Â
- If both the heads are NULL return null.
- If one head is null return the other.
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def newNode(key):
temp = Node(key)
temp.data = key
temp. next = None
return temp
def printList(node):
while (node ! = None ):
print (node.data,
end = " " )
node = node. next
def merge(h1, h2):
if (h1 = = None ):
return h2
if (h2 = = None ):
return h1
if (h1.data < h2.data):
h1. next = merge(h1. next , h2)
return h1
else :
h2. next = merge(h1, h2. next )
return h2
if __name__ = = '__main__' :
head1 = newNode( 1 )
head1. next = newNode( 3 )
head1. next . next = newNode( 5 )
head2 = newNode( 0 )
head2. next = newNode( 2 )
head2. next . next = newNode( 4 )
mergedhead = merge(head1, head2)
printList(mergedhead)
|
Output:Â Â
0 1 2 3 4 5
Complexity Analysis:
- Time complexity:O(n).Â
Only one traversal of the linked lists are needed.
- Auxiliary Space:O(n).Â
If the recursive stack space is taken into consideration.
Method 2 (Iterative):
Approach: This approach is very similar to the above recursive approach.
- Traverse the list from start to end.
- If the head node of second list lies in between two nodes of the first list, insert it there and make the next node of second list the head. Continue this until there is no node left in both lists, i.e. both the lists are traversed.
- If the first list has reached end while traversing, point the next node to the head of second list.
Note: Compare both the lists where the list with a smaller head value is the first list.
Python
class Node:
def __init__( self , data):
self .data = data
self . next = None
def newNode(key):
temp = Node( 0 )
temp.data = key
temp. next = None
return temp
def printList(node):
while (node ! = None ) :
print ( node.data, end = " " )
node = node. next
def mergeUtil(h1, h2):
if (h1. next = = None ) :
h1. next = h2
return h1
curr1 = h1
next1 = h1. next
curr2 = h2
next2 = h2. next
while (next1 ! = None and
curr2 ! = None ):
if ((curr2.data) > = (curr1.data) and
(curr2.data) < = (next1.data)):
next2 = curr2. next
curr1. next = curr2
curr2. next = next1
curr1 = curr2
curr2 = next2
else :
if (next1. next ) :
next1 = next1. next
curr1 = curr1. next
else :
next1. next = curr2
return h1
return h1
def merge(h1, h2):
if (h1 = = None ):
return h2
if (h2 = = None ):
return h1
if (h1.data < h2.data):
return mergeUtil(h1, h2)
else :
return mergeUtil(h2, h1)
head1 = newNode( 1 )
head1. next = newNode( 3 )
head1. next . next = newNode( 5 )
head2 = newNode( 0 )
head2. next = newNode( 2 )
head2. next . next = newNode( 4 )
mergedhead = merge(head1, head2)
printList(mergedhead)
|
Output:Â Â
0 1 2 3 4 5
Complexity Analysis:
- Time complexity:O(n).Â
As only one traversal of the linked lists is needed.
- Auxiliary Space:O(1).Â
As there is no space required.
Please refer complete article on Merge two sorted lists (in-place) for more details!
Last Updated :
11 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...