Python Program For In-Place Merge Two Linked Lists Without Changing Links Of First List
Last Updated :
18 May, 2022
Given two sorted singly linked lists having n and m elements each, merge them using constant space. First, n smallest elements in both the lists should become part of the first list and the rest elements should be part of the second list. Sorted order should be maintained. We are not allowed to change pointers of the first linked list.
Example:
Input:
First List: 2->4->7->8->10
Second List: 1->3->12
Output:
First List: 1->2->3->4->7
Second List: 8->10->12
We strongly recommend you to minimize your browser and try this yourself first.
The problem becomes very simple if we’re allowed to change pointers of the first linked list. If we are allowed to change links, we can simply do something like a merge of the merge-sort algorithm. We assign the first n smallest elements to the first linked list where n is the number of elements in the first linked list and the rest to the second linked list. We can achieve this in O(m + n) time and O(1) space, but this solution violates the requirement that we can't change links of the first list.
The problem becomes a little tricky as we're not allowed to change pointers in the first linked list. The idea is something similar to this post but as we are given a singly linked list, we can't proceed backward with the last element of LL2.
The idea is for each element of LL1, we compare it with the first element of LL2. If LL1 has a greater element than the first element of LL2, then we swap the two elements involved. To keep LL2 sorted, we need to place the first element of LL2 at its correct position. We can find a mismatch by traversing LL2 once and correcting the pointers.
Below is the implementation of this idea.
Python3
# Python3 program to merge two sorted linked
# lists without using any extra space and
# without changing links of first list
# Structure for a linked list node
class Node:
def __init__(self):
self.data = 0
self.next = None
# Given a reference (pointer to pointer) to
# the head of a list and an int, push a new
# node on the front of the list.
def push(head_ref, new_data):
# Allocate node
new_node = Node()
# Put in the data
new_node.data = new_data
# Link the old list off the
# new node
new_node.next = (head_ref)
# Move the head to point to
# the new node
(head_ref) = new_node
return head_ref
# Function to merge two sorted linked lists
# LL1 and LL2 without using any extra space.
def mergeLists(a, b):
# Run till either one of a
# or b runs out
while (a and b):
# For each element of LL1, compare
# it with first element of LL2.
if (a.data > b.data):
# Swap the two elements involved
# if LL1 has a greater element
a.data, b.data = b.data, a.data
temp = b
# To keep LL2 sorted, place first
# element of LL2 at its correct place
if (b.next and b.data > b.next.data):
b = b.next
ptr = b
prev = None
# Find mismatch by traversing the
# second linked list once
while (ptr and ptr.data < temp.data):
prev = ptr
ptr = ptr.next
# Correct the pointers
prev.next = temp
temp.next = ptr
# Move LL1 pointer to next element
a = a.next
# Function to print the linked link
def printList(head):
while (head):
print(head.data, end = '->')
head = head.next
print('NULL')
# Driver code
if __name__=='__main__':
a = None
a = push(a, 10)
a = push(a, 8)
a = push(a, 7)
a = push(a, 4)
a = push(a, 2)
b = None
b = push(b, 12)
b = push(b, 3)
b = push(b, 1)
mergeLists(a, b)
print("First List: ",
end = '')
printList(a)
print("Second List: ",
end = '')
printList(b)
# This code is contributed by rutvik_56
Output :
First List: 1->2->3->4->7->NULL
Second List: 8->10->12->NULL
Time Complexity : O(mn)
Auxiliary Space: O(1)
Please refer complete article on In-place Merge two linked lists without changing links of first list for more details!
Similar Reads
Python Program For Pairwise Swapping Elements Of A Given Linked List By Changing Links Given a singly linked list, write a function to swap elements pairwise. For example, if the linked list is 1->2->3->4->5->6->7 then the function should change it to 2->1->4->3->6->5->7, and if the linked list is 1->2->3->4->5->6 then the function sh
4 min read
Python Program To Merge A Linked List Into Another Linked List At Alternate Positions Given two linked lists, insert nodes of the second list into the first list at alternate positions of the first list. For example, if first list is 5->7->17->13->11 and second is 12->10->2->4->6, the first list should become 5->12->7->10->17->2->13->4-
3 min read
Python Program For Merge Sort Of Linked Lists Merge sort is often preferred for sorting a linked list. The slow random-access performance of a linked list makes some other algorithms (such as quicksort) perform poorly, and others (such as heapsort) completely impossible. Let the head be the first node of the linked list to be sorted and headRe
6 min read
Python Program For Finding Intersection Point Of Two Linked Lists There are two singly linked lists in a system. By some programming error, the end node of one of the linked list got linked to the second list, forming an inverted Y-shaped list. Write a program to get the point where two linked lists merge. Above diagram shows an example with two linked lists havi
6 min read
Python Program For Finding Intersection Of Two Sorted Linked Lists Given two lists sorted in increasing order, create and return a new list representing the intersection of the two lists. The new list should be made with its own memory â the original lists should not be changed. Example: Input: First linked list: 1->2->3->4->6 Second linked list be 2->4->6->8, Ou
4 min read
Python Program For Swapping Nodes In A Linked List Without Swapping Data Given a linked list and two keys in it, swap nodes for two given keys. Nodes should be swapped by changing links. Swapping data of nodes may be expensive in many situations when data contains many fields. It may be assumed that all keys in the linked list are distinct. Examples: Input : 10->15-
5 min read