Given two unsorted Linked List, the task is to merge them to get a sorted singly linked list.
Examples:
Input: List 1 = 3 -> 1 -> 5, List 2 = 6-> 2 -> 4
Output: 1 -> 2 -> 3 -> 4 -> 5 -> 6
Input: List 1 = 4 -> 7 -> 5, List 2 = 2-> 1 -> 8 -> 1
Output: 1 -> 1 -> 2 -> 4 -> 5 -> 7 -> 8
Naive Approach: The naive approach is to sort the given linked lists and then merge the two sorted linked lists together into one list in increasing order.
To solve the problem mentioned above the naive method is to sort the two linked lists individually and merge the two linked lists together into one list which is in increasing order.
Efficient Approach: To optimize the above method we will concatenate the two linked lists and then sort it using any sorting algorithm. Below are the steps:
- Concatenate the two lists by traversing the first list until we reach it’s a tail node and then point the next of the tail node to the head node of the second list. Store this concatenated list in the first list.
- Sort the above-merged linked list. Here, we will use a bubble sort. So, if node->next->data is less then node->data, then swap the data of the two adjacent nodes.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct node {
int data;
node* next;
};
void setData(node* head)
{
node* tmp;
tmp = head;
while (tmp != NULL) {
cout << tmp->data
<< " -> " ;
tmp = tmp->next;
}
}
node* getData(node* head, int num)
{
node* temp = new node;
node* tail = head;
temp->data = num;
temp->next = NULL;
if (head == NULL) {
head = temp;
tail = temp;
}
else {
while (tail != NULL) {
if (tail->next == NULL) {
tail->next = temp;
tail = tail->next;
}
tail = tail->next;
}
}
return head;
}
node* mergelists(node** head1,
node** head2)
{
node* tail = *head1;
while (tail != NULL) {
if (tail->next == NULL
&& head2 != NULL) {
tail->next = *head2;
break ;
}
tail = tail->next;
}
return *head1;
}
void sortlist(node** head1)
{
node* curr = *head1;
node* temp = *head1;
while (curr->next != NULL) {
temp = curr->next;
while (temp != NULL) {
if (temp->data < curr->data) {
int t = temp->data;
temp->data = curr->data;
curr->data = t;
}
temp = temp->next;
}
curr = curr->next;
}
}
int main()
{
node* head1 = new node;
node* head2 = new node;
head1 = NULL;
head2 = NULL;
head1 = getData(head1, 4);
head1 = getData(head1, 7);
head1 = getData(head1, 5);
head2 = getData(head2, 2);
head2 = getData(head2, 1);
head2 = getData(head2, 8);
head2 = getData(head2, 1);
head1 = mergelists(&head1,
&head2);
sortlist(&head1);
setData(head1);
return 0;
}
|
Java
class GFG{
static node head1 = null ;
static node head2 = null ;
static class node
{
int data;
node next;
};
static void setData(node head)
{
node tmp;
tmp = head;
while (tmp != null )
{
System.out.print(tmp.data + " -> " );
tmp = tmp.next;
}
}
static node getData(node head, int num)
{
node temp = new node();
node tail = head;
temp.data = num;
temp.next = null ;
if (head == null )
{
head = temp;
tail = temp;
}
else
{
while (tail != null )
{
if (tail.next == null )
{
tail.next = temp;
tail = tail.next;
}
tail = tail.next;
}
}
return head;
}
static node mergelists()
{
node tail = head1;
while (tail != null )
{
if (tail.next == null &&
head2 != null )
{
tail.next = head2;
break ;
}
tail = tail.next;
}
return head1;
}
static void sortlist()
{
node curr = head1;
node temp = head1;
while (curr.next != null )
{
temp = curr.next;
while (temp != null )
{
if (temp.data < curr.data)
{
int t = temp.data;
temp.data = curr.data;
curr.data = t;
}
temp = temp.next;
}
curr = curr.next;
}
}
public static void main(String[] args)
{
head1 = getData(head1, 4 );
head1 = getData(head1, 7 );
head1 = getData(head1, 5 );
head2 = getData(head2, 2 );
head2 = getData(head2, 1 );
head2 = getData(head2, 8 );
head2 = getData(head2, 1 );
head1 = mergelists();
sortlist();
setData(head1);
}
}
|
Python3
class node:
def __init__( self , x):
self .data = x
self . next = None
def setData(head):
tmp = head
while (tmp ! = None ):
print (tmp.data,
end = " -> " )
tmp = tmp. next
def getData(head, num):
temp = node( - 1 )
tail = head
temp.data = num
temp. next = None
if (head = = None ):
head = temp
tail = temp
else :
while (tail ! = None ):
if (tail. next = = None ):
tail. next = temp
tail = tail. next
tail = tail. next
return head
def mergelists(head1,head2):
tail = head1
while (tail ! = None ):
if (tail. next = = None
and head2 ! = None ):
tail. next = head2
break
tail = tail. next
return head1
def sortlist(head1):
curr = head1
temp = head1
while (curr. next ! = None ):
temp = curr. next
while (temp ! = None ):
if (temp.data < curr.data):
t = temp.data
temp.data = curr.data
curr.data = t
temp = temp. next
curr = curr. next
if __name__ = = '__main__' :
head1 = node( - 1 )
head2 = node( - 1 )
head1 = None
head2 = None
head1 = getData(head1, 4 )
head1 = getData(head1, 7 )
head1 = getData(head1, 5 )
head2 = getData(head2, 2 )
head2 = getData(head2, 1 )
head2 = getData(head2, 8 )
head2 = getData(head2, 1 )
head1 = mergelists(head1,head2)
sortlist(head1)
setData(head1)
|
C#
using System;
class GFG{
static node head1 = null ;
static node head2 = null ;
class node
{
public int data;
public node next;
};
static void setData(node head)
{
node tmp;
tmp = head;
while (tmp != null )
{
Console.Write(tmp.data + " -> " );
tmp = tmp.next;
}
}
static node getData(node head, int num)
{
node temp = new node();
node tail = head;
temp.data = num;
temp.next = null ;
if (head == null )
{
head = temp;
tail = temp;
}
else
{
while (tail != null )
{
if (tail.next == null )
{
tail.next = temp;
tail = tail.next;
}
tail = tail.next;
}
}
return head;
}
static node mergelists()
{
node tail = head1;
while (tail != null )
{
if (tail.next == null &&
head2 != null )
{
tail.next = head2;
break ;
}
tail = tail.next;
}
return head1;
}
static void sortlist()
{
node curr = head1;
node temp = head1;
while (curr.next != null )
{
temp = curr.next;
while (temp != null )
{
if (temp.data < curr.data)
{
int t = temp.data;
temp.data = curr.data;
curr.data = t;
}
temp = temp.next;
}
curr = curr.next;
}
}
public static void Main(String[] args)
{
head1 = getData(head1, 4);
head1 = getData(head1, 7);
head1 = getData(head1, 5);
head2 = getData(head2, 2);
head2 = getData(head2, 1);
head2 = getData(head2, 8);
head2 = getData(head2, 1);
head1 = mergelists();
sortlist();
setData(head1);
}
}
|
Javascript
<script>
var head1 = null ;
var head2 = null ;
class node {
constructor(){
this .data=0;
this .next = null ;
}
}
function setData( head) {
var tmp;
tmp = head;
while (tmp != null ) {
document.write(tmp.data + " -> " );
tmp = tmp.next;
}
}
function getData( head , num) {
temp = new node();
var tail = head;
temp.data = num;
temp.next = null ;
if (head == null ) {
head = temp;
tail = temp;
}
else {
while (tail != null ) {
if (tail.next == null ) {
tail.next = temp;
tail = tail.next;
}
tail = tail.next;
}
}
return head;
}
function mergelists() {
tail = head1;
while (tail != null ) {
if (tail.next == null && head2 != null ) {
tail.next = head2;
break ;
}
tail = tail.next;
}
return head1;
}
function sortlist() {
curr = head1;
temp = head1;
while (curr.next != null ) {
temp = curr.next;
while (temp != null ) {
if (temp.data < curr.data) {
var t = temp.data;
temp.data = curr.data;
curr.data = t;
}
temp = temp.next;
}
curr = curr.next;
}
}
head1 = getData(head1, 4);
head1 = getData(head1, 7);
head1 = getData(head1, 5);
head2 = getData(head2, 2);
head2 = getData(head2, 1);
head2 = getData(head2, 8);
head2 = getData(head2, 1);
head1 = mergelists();
sortlist();
setData(head1);
</script>
|
Output:
1 -> 1 -> 2 -> 4 -> 5 -> 7 -> 8
Time Complexity: O((M+N)^2) where M and N are the lengths of the two given linked lists. We are merging the two list and performing bubble sort on the merged list. The time complexity of bubble sort is quadratic.
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
27 Dec, 2021
Like Article
Save Article