Merge two sorted linked list without duplicates
Last Updated :
28 Nov, 2023
Merge two sorted linked list of size n1 and n2. The duplicates in two linked list should be present only once in the final sorted linked list.
Examples:
Input : list1: 1->1->4->5->7
list2: 2->4->7->9
Output : 1 2 4 5 7 9
Source: Microsoft on Campus Placement and Interview Questions
Approach: Following are the steps:
- Merge the two sorted linked list in sorted manner. Refer recursive approach of this post. Let the final obtained list be head.
- Remove duplicates from sorted linked list head.
Implementation:
C++
// C++ implementation to merge two sorted linked list
// without duplicates
#include <bits/stdc++.h>
using namespace std;
// structure of a node
struct Node {
int data;
Node* next;
};
// function to get a new node
Node* getNode(int data)
{
// allocate space
Node* temp = (Node*)malloc(sizeof(Node));
// put in data
temp->data = data;
temp->next = NULL;
return temp;
}
// function to merge two sorted linked list
// in a sorted manner
Node* sortedMerge(struct Node* a, struct Node* b)
{
Node* result = NULL;
/* Base cases */
if (a == NULL)
return (b);
else if (b == NULL)
return (a);
/* Pick either a or b, and recur */
if (a->data <= b->data) {
result = a;
result->next = sortedMerge(a->next, b);
}
else {
result = b;
result->next = sortedMerge(a, b->next);
}
return (result);
}
/* The function removes duplicates from a sorted list */
void removeDuplicates(Node* head)
{
/* Pointer to traverse the linked list */
Node* current = head;
/* Pointer to store the next pointer of a node to be deleted*/
Node* next_next;
/* do nothing if the list is empty */
if (current == NULL)
return;
/* Traverse the list till last node */
while (current->next != NULL) {
/* Compare current node with next node */
if (current->data == current->next->data) {
/* The sequence of steps is important*/
next_next = current->next->next;
free(current->next);
current->next = next_next;
}
else /* This is tricky: only advance if no deletion */
{
current = current->next;
}
}
}
// function to merge two sorted linked list
// without duplicates
Node* sortedMergeWithoutDuplicates(Node* head1, Node* head2)
{
// merge two linked list in sorted manner
Node* head = sortedMerge(head1, head2);
// remove duplicates from the list 'head'
removeDuplicates(head);
return head;
}
// function to print the linked list
void printList(Node* head)
{
while (head != NULL) {
cout << head->data << " ";
head = head->next;
}
}
// Driver program to test above
int main()
{
// head1: 1->1->4->5->7
Node* head1 = getNode(1);
head1->next = getNode(1);
head1->next->next = getNode(4);
head1->next->next->next = getNode(5);
head1->next->next->next->next = getNode(7);
// head2: 2->4->7->9
Node* head2 = getNode(2);
head2->next = getNode(4);
head2->next->next = getNode(7);
head2->next->next->next = getNode(9);
Node* head3;
head3 = sortedMergeWithoutDuplicates(head1, head2);
printList(head3);
return 0;
}
Java
// Java implementation to merge two sorted linked list
// without duplicates
import java.io.*;
class GFG {
// structure of a node
class Node {
int data;
Node next;
}
// function to get a new node
public Node getNode(int data)
{
Node temp = new Node();
temp.data = data;
temp.next = null;
return temp;
}
// function to merge two sorted linked list in a sorted
// manner
static Node sortedMerge(Node a, Node b)
{
Node result = null;
/* Base cases */
if (a == null) {
return b;
}
else if (b == null) {
return a;
}
/* Pick either a or b, and recur */
if (a.data <= b.data) {
result = a;
result.next = sortedMerge(a.next, b);
}
else {
result = b;
result.next = sortedMerge(a, b.next);
}
return result;
}
/* The function removes duplicates from a sorted list */
static void removeDuplicates(Node head)
{
/* Pointer to traverse the linked list */
Node current = head;
/* Pointer to store the next pointer of a node to be
* deleted*/
Node next_next;
/* do nothing if the list is empty */
if (current == null) {
return;
}
/* Traverse the list till last node */
while (current.next != null)
{
/* Compare current node with next node */
if (current.data == current.next.data)
{
/* The sequence of steps is important*/
next_next = current.next.next;
current.next = next_next;
}
else { /* This is tricky: only advance if no
deletion */
current = current.next;
}
}
}
// function to merge two sorted linked list without
// duplicates
public Node sortedMergeWithoutDuplicates(Node head1,
Node head2)
{
// merge two linked list in sorted manner
Node head = sortedMerge(head1, head2);
// remove duplicates from the list 'head'
removeDuplicates(head);
return head;
}
// function to print the linked list
public void printList(Node head)
{
while (head != null) {
System.out.print(head.data + " ");
head = head.next;
}
}
public static void main(String[] args)
{
GFG l = new GFG();
// head1 : 1->1->4->5->7
Node head1 = l.getNode(1);
head1.next = l.getNode(1);
head1.next.next = l.getNode(4);
head1.next.next.next = l.getNode(5);
head1.next.next.next.next = l.getNode(7);
// head2 : 2->4->7->9
Node head2 = l.getNode(2);
head2.next = l.getNode(4);
head2.next.next = l.getNode(7);
head2.next.next.next = l.getNode(9);
Node head3;
head3
= l.sortedMergeWithoutDuplicates(head1, head2);
l.printList(head3);
}
}
// This code is contributed by lokeshmvs21.
Python3
# Python3 implementation to merge two
# sorted linked list without duplicates
# Structure of a node
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Function to get a new node
def getNode(data):
# Allocate space
temp = Node(data)
return temp
# Function to merge two sorted linked
# list in a sorted manner
def sortedMerge(a, b):
result = None
# Base cases
if (a == None):
return(b)
elif (b == None):
return(a)
# Pick either a or b, and recur
if (a.data <= b.data):
result = a
result.next = sortedMerge(a.next, b)
else:
result = b
result.next = sortedMerge(a, b.next)
return(result)
# The function removes duplicates
# from a sorted list
def removeDuplicates(head):
# Pointer to traverse the linked list
current = head
# Pointer to store the next pointer
# of a node to be deleted
next_next = None
# Do nothing if the list is empty
if (current == None):
return
# Traverse the list till last node
while (current.next != None):
# Compare current node with next node
if (current.data == current.next.data):
# The sequence of steps is important
next_next = current.next.next
del (current.next)
current.next = next_next
else:
# This is tricky: only advance
# if no deletion
current = current.next
# Function to merge two sorted linked list
# without duplicates
def sortedMergeWithoutDuplicates(head1, head2):
# Merge two linked list in sorted manner
head = sortedMerge(head1, head2)
# Remove duplicates from the list 'head'
removeDuplicates(head)
return head
# Function to print the linked list
def printList(head):
while (head != None):
print(head.data, end = ' ')
head = head.next
# Driver code
if __name__=='__main__':
# head1: 1.1.4.5.7
head1 = getNode(1)
head1.next = getNode(1)
head1.next.next = getNode(4)
head1.next.next.next = getNode(5)
head1.next.next.next.next = getNode(7)
# head2: 2.4.7.9
head2 = getNode(2)
head2.next = getNode(4)
head2.next.next = getNode(7)
head2.next.next.next = getNode(9)
head3 = sortedMergeWithoutDuplicates(
head1, head2)
printList(head3)
# This code is contributed by rutvik_56
C#
// C# implementation to merge two sorted linked list
// without duplicates
using System;
public class GFG{
// structure of a node
class Node {
public int data;
public Node next;
}
// function to get a new node
Node getNode(int data)
{
Node temp = new Node();
temp.data = data;
temp.next = null;
return temp;
}
// function to merge two sorted linked list in a sorted
// manner
static Node sortedMerge(Node a, Node b)
{
Node result = null;
/* Base cases */
if (a == null) {
return b;
}
else if (b == null) {
return a;
}
/* Pick either a or b, and recur */
if (a.data <= b.data) {
result = a;
result.next = sortedMerge(a.next, b);
}
else {
result = b;
result.next = sortedMerge(a, b.next);
}
return result;
}
/* The function removes duplicates from a sorted list */
static void removeDuplicates(Node head)
{
/* Pointer to traverse the linked list */
Node current = head;
/* Pointer to store the next pointer of a node to be
* deleted*/
Node next_next;
/* do nothing if the list is empty */
if (current == null) {
return;
}
/* Traverse the list till last node */
while (current.next != null)
{
/* Compare current node with next node */
if (current.data == current.next.data)
{
/* The sequence of steps is important*/
next_next = current.next.next;
current.next = next_next;
}
else { /* This is tricky: only advance if no
deletion */
current = current.next;
}
}
}
// function to merge two sorted linked list without
// duplicates
Node sortedMergeWithoutDuplicates(Node head1,
Node head2)
{
// merge two linked list in sorted manner
Node head = sortedMerge(head1, head2);
// remove duplicates from the list 'head'
removeDuplicates(head);
return head;
}
// function to print the linked list
void printList(Node head)
{
while (head != null) {
Console.Write(head.data + " ");
head = head.next;
}
}
static public void Main (){
GFG l = new GFG();
// head1 : 1->1->4->5->7
Node head1 = l.getNode(1);
head1.next = l.getNode(1);
head1.next.next = l.getNode(4);
head1.next.next.next = l.getNode(5);
head1.next.next.next.next = l.getNode(7);
// head2 : 2->4->7->9
Node head2 = l.getNode(2);
head2.next = l.getNode(4);
head2.next.next = l.getNode(7);
head2.next.next.next = l.getNode(9);
Node head3;
head3
= l.sortedMergeWithoutDuplicates(head1, head2);
l.printList(head3);
}
}
// This code is contributed by lokeshmvs21.
JavaScript
// JavaScript implementation to merge two sorted linked list
// without duplicates
// structure to get a new node
class Node{
constructor(data){
this.data = data;
this.next = null;
}
}
// function to get a new node
function getNode(data){
// allocate space and put data
let temp = new Node(data);
return temp;
}
// function to merge two sorted linked list
// in a sorted manner
function sortedMerge(a, b){
let result = null;
// base case
if(a == null)
return b;
else if(b == null)
return a;
// Pick either a or b, and recur
if (a.data <= b.data) {
result = a;
result.next = sortedMerge(a.next, b);
}
else {
result = b;
result.next = sortedMerge(a, b.next);
}
return result;
}
// The function removes duplicates from a sorted list
function removeDuplicates(head){
// Pointer to traverse the linked list
let current = head;
// Pointer to store the next pointer of a node to be deleted
let next_next;
// do nothing if the list is empty
if (current == null)
return;
// Traverse the list till last node
while (current.next != null) {
// Compare current node with next node
if (current.data == current.next.data) {
// The sequence of steps is important
next_next = current.next.next;
current.next = next_next;
}
else // This is tricky: only advance if no deletion
{
current = current.next;
}
}
}
// function to merge two sorted linked list
// without duplicates
function sortedMergeWithoutDuplicates(head1, head2){
// merge two linked list in sorted manner
let head = sortedMerge(head1, head2);
// remove duplicates from the list 'head'
removeDuplicates(head);
return head;
}
// function to print the linked list
function printList(head)
{
while (head != null) {
console.log(head.data + " ");
head = head.next;
}
}
// Driver program to test above
// head1: 1->1->4->5->7
let head1 = getNode(1);
head1.next = getNode(1);
head1.next.next = getNode(4);
head1.next.next.next = getNode(5);
head1.next.next.next.next = getNode(7);
// head2: 2->4->7->9
let head2 = getNode(2);
head2.next = getNode(4);
head2.next.next = getNode(7);
head2.next.next.next = getNode(9);
let head3 = sortedMergeWithoutDuplicates(head1, head2);
printList(head3);
// This code is contributed by Yash Agarwal(yashagawral2852002)
Complexity Analysis:
- Time complexity: O(n1 + n2).
- Auxiliary Space: O(1).
Approach: This approach uses an iterative approach to merge two sorted linked lists without duplicates.
Steps:
- It creates a dummy node and a tail pointer to efficiently merge the two lists in sorted order. The dummy node helps in handling the merged list easily.
- As the lists are merged, it simultaneously removes duplicates in a single pass through the merged list.
Below is the implementation of the above approach:
C++
//C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* getNode(int data)
{
Node* temp = new Node;
temp->data = data;
temp->next = NULL;
return temp;
}
Node* sortedMergeWithoutDuplicates(Node* head1, Node* head2)
{
// Create a dummy node to simplify merging
Node* dummy = new Node;
Node* tail = dummy;
// Merge the two sorted linked lists
while (head1 != NULL && head2 != NULL) {
if (head1->data < head2->data) {
// Append the smaller node to the merged list
tail->next = head1;
head1 = head1->next;
}
else if (head1->data > head2->data) {
// Append the smaller node to the merged list
tail->next = head2;
head2 = head2->next;
}
else {
// If both nodes have the same value, append only one of them to avoid duplicates
tail->next = head1;
head1 = head1->next;
head2 = head2->next;
}
tail = tail->next;
tail->next = NULL; // Mark the end of the merged list
}
// If any elements left in either of the lists, add them to the merged list
if (head1 != NULL) {
tail->next = head1;
}
if (head2 != NULL) {
tail->next = head2;
}
// Remove duplicates from the merged list
Node* current = dummy->next;
while (current != NULL && current->next != NULL) {
if (current->data == current->next->data) {
// Remove the duplicate node from the merged list
Node* temp = current->next;
current->next = current->next->next;
delete temp; // Free the memory of the duplicate node
}
else {
// Move to the next node
current = current->next;
}
}
Node* result = dummy->next;
delete dummy; // Free the memory of the dummy node
return result;
}
void printList(Node* head)
{
// Traverse the linked list and print its elements
while (head != NULL) {
cout << head->data << " ";
head = head->next;
}
cout << endl;
}
int main()
{
// Create two sorted linked lists
Node* head1 = getNode(1);
head1->next = getNode(1);
head1->next->next = getNode(4);
head1->next->next->next = getNode(5);
head1->next->next->next->next = getNode(7);
Node* head2 = getNode(2);
head2->next = getNode(4);
head2->next->next = getNode(7);
head2->next->next->next = getNode(9);
// Merge the two lists without duplicates and print the result
Node* head3 = sortedMergeWithoutDuplicates(head1, head2);
printList(head3);
return 0;
}
Java
public class Main {
// Node class to represent a node in the linked list
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
// Function to merge two sorted linked lists without duplicates
static Node sortedMergeWithoutDuplicates(Node head1, Node head2) {
// Create a dummy node to simplify merging
Node dummy = new Node(-1);
Node tail = dummy;
// Merge the two sorted linked lists
while (head1 != null && head2 != null) {
if (head1.data < head2.data) {
// Append the smaller node to the merged list
tail.next = head1;
head1 = head1.next;
} else if (head1.data > head2.data) {
// Append the smaller node to the merged list
tail.next = head2;
head2 = head2.next;
} else {
// If both nodes have the same value,
// append only one of them to avoid duplicates
tail.next = head1;
head1 = head1.next;
head2 = head2.next;
}
tail = tail.next;
tail.next = null; // Mark the end of the merged list
}
// If any elements left in either of the lists, add them to the merged list
if (head1 != null) {
tail.next = head1;
}
if (head2 != null) {
tail.next = head2;
}
// Remove duplicates from the merged list
Node current = dummy.next;
while (current != null && current.next != null) {
if (current.data == current.next.data) {
// Remove the duplicate node from the merged list
Node temp = current.next;
current.next = current.next.next;
temp = null; // Free the memory of the duplicate node
} else {
// Move to the next node
current = current.next;
}
}
return dummy.next;
}
// Function to print a linked list
static void printList(Node head) {
// Traverse the linked list and print its elements
while (head != null) {
System.out.print(head.data + " ");
head = head.next;
}
System.out.println();
}
public static void main(String[] args) {
// Create two sorted linked lists
Node head1 = new Node(1);
head1.next = new Node(1);
head1.next.next = new Node(4);
head1.next.next.next = new Node(5);
head1.next.next.next.next = new Node(7);
Node head2 = new Node(2);
head2.next = new Node(4);
head2.next.next = new Node(7);
head2.next.next.next = new Node(9);
// Merge the two lists without duplicates and print the result
Node head3 = sortedMergeWithoutDuplicates(head1, head2);
printList(head3);
}
}
// This code is contributed by rambabuguphka
Python3
class Node:
def __init__(self, data):
self.data = data
self.next = None
def sortedMergeWithoutDuplicates(head1, head2):
dummy = Node(0)
tail = dummy
while head1 is not None and head2 is not None:
if head1.data < head2.data:
tail.next = head1
head1 = head1.next
elif head1.data > head2.data:
tail.next = head2
head2 = head2.next
else:
# Skip duplicates
head1 = head1.next
head2 = head2.next
tail = tail.next
if head1 is not None:
tail.next = head1
if head2 is not None:
tail.next = head2
current = dummy.next
while current is not None and current.next is not None:
if current.data == current.next.data:
temp = current.next
current.next = current.next.next
del temp
else:
current = current.next
result = dummy.next
return result
def printList(head):
while head is not None:
print(head.data, end=" ")
head = head.next
print()
if __name__ == "__main__":
# Create two sorted linked lists
head1 = Node(1)
head1.next = Node(1)
head1.next.next = Node(4)
head1.next.next.next = Node(5)
head1.next.next.next.next = Node(7)
head2 = Node(2)
head2.next = Node(4)
head2.next.next = Node(7)
head2.next.next.next = Node(9)
# Merge the two lists without duplicates and print the result
head3 = sortedMergeWithoutDuplicates(head1, head2)
printList(head3)
C#
// C# code for the above approach
using System;
public class Node {
public int data;
public Node next;
}
public class GFG {
// Function to create a new node
static Node GetNode(int data)
{
Node temp = new Node();
temp.data = data;
temp.next = null;
return temp;
}
// Function to merge two sorted linked lists without
// duplicates
static Node SortedMergeWithoutDuplicates(Node head1,
Node head2)
{
// Create a dummy node to simplify merging
Node dummy = new Node();
Node tail = dummy;
// Merge the two sorted linked lists
while (head1 != null && head2 != null) {
if (head1.data < head2.data) {
// Append the smaller node to the merged
// list
tail.next = head1;
head1 = head1.next;
}
else if (head1.data > head2.data) {
// Append the smaller node to the merged
// list
tail.next = head2;
head2 = head2.next;
}
else {
// If both nodes have the same value, append
// only one of them to avoid duplicates
tail.next = head1;
head1 = head1.next;
head2 = head2.next;
}
tail = tail.next;
tail.next
= null; // Mark the end of the merged list
}
// If any elements left in either of the lists, add
// them to the merged list
if (head1 != null) {
tail.next = head1;
}
if (head2 != null) {
tail.next = head2;
}
// Remove duplicates from the merged list
Node current = dummy.next;
while (current != null && current.next != null) {
if (current.data == current.next.data) {
// Remove the duplicate node from the merged
// list
_ = current.next;
current.next = current.next.next;
// Free the memory of the duplicate node
_ = null;
}
else {
// Move to the next node
current = current.next;
}
}
Node result = dummy.next;
// Free the memory of the dummy node
dummy = null;
return result;
}
// Function to print the linked list
static void PrintList(Node head)
{
// Traverse the linked list and print its elements
while (head != null) {
Console.Write(head.data + " ");
head = head.next;
}
Console.WriteLine();
}
// Main method
public static void Main(string[] args)
{
// Create two sorted linked lists
Node head1 = GetNode(1);
head1.next = GetNode(1);
head1.next.next = GetNode(4);
head1.next.next.next = GetNode(5);
head1.next.next.next.next = GetNode(7);
Node head2 = GetNode(2);
head2.next = GetNode(4);
head2.next.next = GetNode(7);
head2.next.next.next = GetNode(9);
// Merge the two lists without duplicates and print
// the result
Node head3
= SortedMergeWithoutDuplicates(head1, head2);
PrintList(head3);
}
}
// This code is contributed by Susobhan Akhuli
JavaScript
<script>
// JavaScript code for the above approach
// Definition of the Node structure
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// Function to get a new Node with the given data
function getNode(data) {
let temp = new Node(data);
return temp;
}
// Function to merge two sorted linked lists without duplicates
function sortedMergeWithoutDuplicates(head1, head2) {
// Create a dummy node to simplify merging
let dummy = new Node();
let tail = dummy;
// Merge the two sorted linked lists
while (head1 !== null && head2 !== null) {
if (head1.data < head2.data) {
// Append the smaller node to the merged list
tail.next = head1;
head1 = head1.next;
} else if (head1.data > head2.data) {
// Append the smaller node to the merged list
tail.next = head2;
head2 = head2.next;
} else {
// If both nodes have the same value, append only one of them to avoid duplicates
tail.next = head1;
head1 = head1.next;
head2 = head2.next;
}
tail = tail.next;
tail.next = null; // Mark the end of the merged list
}
// If any elements left in either of the lists, add them to the merged list
if (head1 !== null) {
tail.next = head1;
}
if (head2 !== null) {
tail.next = head2;
}
// Remove duplicates from the merged list
let current = dummy.next;
while (current !== null && current.next !== null) {
if (current.data == current.next.data) {
// Remove the duplicate node from the merged list
let temp = current.next;
current.next = current.next.next;
temp = null; // Free the memory of the duplicate node
} else {
// Move to the next node
current = current.next;
}
}
let result = dummy.next;
dummy = null; // Free the memory of the dummy node
return result;
}
// Function to print the linked list
function printList(head) {
// Traverse the linked list and print its elements
while (head !== null) {
document.write(head.data + " ");
head = head.next;
}
}
// Create two sorted linked lists
let head1 = getNode(1);
head1.next = getNode(1);
head1.next.next = getNode(4);
head1.next.next.next = getNode(5);
head1.next.next.next.next = getNode(7);
let head2 = getNode(2);
head2.next = getNode(4);
head2.next.next = getNode(7);
head2.next.next.next = getNode(9);
// Merge the two lists without duplicates and print the result
let head3 = sortedMergeWithoutDuplicates(head1, head2);
printList(head3);
// This code is contributed by Susobhan Akhuli
</script>
Time Complexity: O(M + N), Where M and N are the size of the list1 and list2 respectively.
Auxiliary Space: O(M+N), Function call stack space
Exercise: Get the final sorted linked list without duplicates in a single traversal of the two lists.
Similar Reads
Merge two sorted linked lists
Given two sorted linked lists consisting of n and m nodes respectively. The task is to merge both of the lists and return the head of the merged list.Examples:Input: Output: Input:Output: Table of Content[Naive Approach] By Using Array - O((n+m)*log(n+m)) Time and O(n+m) Space[Better Approach] Using
12 min read
Remove duplicates from a sorted linked list
Given a linked list sorted in non-decreasing order. Return the list by deleting the duplicate nodes from the list. The returned list should also be in non-decreasing order.Example:Input : Linked List = 11->11->11->21->43->43->60Output : 11->21->43->60Explanation:Remove dup
15+ min read
Merge two sorted linked lists using Dummy Nodes
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the lists (in place) and return the head of the merged list.Examples:Input: a: 5->10->15, b: 2->3->20Output: 2->3->5->10->15->20Input: a: 1->1, b: 2->4Output: 1->1
10 min read
Difference of two Linked Lists using Merge sort
Given two Linked List, the task is to create a Linked List to store the difference of Linked List 1 with Linked List 2, i.e. the elements present in List 1 but not in List 2.Examples: Input: List1: 10 -> 15 -> 4 ->20, List2: 8 -> 4 -> 2 -> 10 Output: 15 -> 20 Explanation: In the
14 min read
Replace nodes with duplicates in linked list
Given a linked list that contains some random integers from 1 to n with many duplicates. Replace each duplicate element that is present in the linked list with the values n+1, n+2, n+3 and so on(starting from left to right in the given linked list). Examples: Input : 1 3 1 4 4 2 1 Output : 1 3 5 4 6
9 min read
Union and Intersection of two Linked List using Merge Sort
Given two singly Linked Lists, create union and intersection lists that contain the union and intersection of the elements present in the given lists. Each of the two lists contains distinct node values.Note: The order of elements in output lists doesn't matter.Examples:Input: head1: 10 -> 15 -
15+ min read
Remove Duplicates from an Unsorted Linked List
Given an unsorted linked list containing n nodes, the task is to remove duplicate nodes while preserving the original order.Examples:Input: 12 -> 11 -> 12 -> 21 -> 41 -> 43 -> 21 Output: 12 -> 11 -> 21 -> 41 -> 43 Explanation: The second occurrence of 12 (the one after
14 min read
Merge K sorted linked lists
Given k sorted linked lists of different sizes, the task is to merge them all maintaining their sorted order.Examples: Input: Output: Merged lists in a sorted order where every element is greater than the previous element.Input: Output: Merged lists in a sorted order where every element is greater t
15+ min read
Merge K sorted Doubly Linked List in Sorted Order
Given K sorted doubly linked list. The task is to merge all sorted doubly linked list in single sorted doubly linked list means final list must be sorted.Examples: Input: List 1 : 2 <-> 7 <-> 8 <-> 12 <-> 15 <-> NULL List 2 : 4 <-> 9 <-> 10 <-> NULL Li
15+ min read
Merge two unsorted linked lists to get a sorted list - Set 2
Given two unsorted Linked Lists, L1 of N nodes and L2 of M nodes, the task is to merge them to get a sorted singly linked list. Examples: Input: L1 = 3?5?1, L2 = 6?2?4?9Output: 1?2?3?4?5?6?9 Input: L1 = 1?5?2, L2 = 3?7Output: 1?2?3?5?7 Note: A Memory Efficient approach that solves this problem in O(
15+ min read