C++ Program For In-Place Merge Two Linked Lists Without Changing Links Of First List
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.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node *next;
};
void push( struct Node** head_ref,
int new_data)
{
struct Node* new_node =
( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void mergeLists( struct Node *a,
struct Node * &b)
{
while (a && b)
{
if (a->data > b->data)
{
swap(a->data, b->data);
struct Node *temp = b;
if (b->next && b->data >
b->next->data)
{
b = b->next;
struct Node *ptr= b,
*prev = NULL;
while (ptr && ptr->data <
temp->data)
{
prev = ptr;
ptr = ptr -> next;
}
prev->next = temp;
temp->next = ptr;
}
}
a = a->next;
}
}
void printList( struct Node *head)
{
while (head)
{
cout << head->data << "->" ;
head = head->next;
}
cout << "NULL" << endl;
}
int main()
{
struct Node *a = NULL;
push(&a, 10);
push(&a, 8);
push(&a, 7);
push(&a, 4);
push(&a, 2);
struct Node *b = NULL;
push(&b, 12);
push(&b, 3);
push(&b, 1);
mergeLists(a, b);
cout << "First List: " ;
printList(a);
cout << "Second List: " ;
printList(b);
return 0;
}
|
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!
Last Updated :
31 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...