Merge two sorted linked lists using Dummy Nodes
Last Updated :
17 Aug, 2024
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->20
Output: 2->3->5->10->15->20
Input: a: 1->1, b: 2->4
Output: 1->1->2->4
The idea is to use a temporary dummy node as the start of the result list. The pointer Tail always points to the last node in the result list, so appending new nodes is easy.Follow the below illustration for a better understanding:

Follow the steps below to solve the problem:
- First, make a dummy node for the new merged linked list
- Now make two pointers, one will point to list1 and another will point to list2.
- Now traverse the lists till one of them gets exhausted.
- If the value of the node pointing to either list is smaller than another pointer, add that node to our merged list and increment that pointer.
Below is the implementation of the above approach:
C++
// C++ program to merge two sorted linked lists
// using dummy nodes
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int new_data)
{
this->data = new_data;
this->next = nullptr;
}
};
// Function that takes two lists sorted in increasing order,
// and splices their nodes together to make one big sorted
// list which is returned.
Node* mergeSortedList(Node* first, Node* second) {
// A dummy first node to hang the result on
Node dummy(-1);
// Tail points to the last result node
Node* tail = &dummy;
// So tail->next is the place to add new nodes to the
// result
while (1) {
if (first == NULL) {
// If either list runs out, use the other list
tail->next = second;
break;
}
else if (first == NULL) {
tail->next = first;
break;
}
if (first->data <= second->data) {
// Move the front node from 'a' to the result
// list
Node* newNode = first;
first = first->next;
newNode->next = tail->next;
tail->next = newNode;
}
else {
// Move the front node from 'b' to the result
// list
Node* newNode = second;
second = second->next;
newNode->next = tail->next;
tail->next = newNode;
}
tail = tail->next;
}
return (dummy.next);
}
// Driver code
int main() {
// Create a hard-coded linked list:
// 2 -> 4 -> 8 -> 9
Node* first = new Node(2);
first->next = new Node(4);
first->next->next = new Node(8);
first->next->next->next = new Node(9);
// Create another hard-coded linked list:
// 1 -> 3 -> 8 -> 10
Node* second = new Node(1);
second->next = new Node(3);
second->next->next = new Node(8);
second->next->next->next = new Node(10);
Node* mergedList = mergeSortedList(first, second);
Node* temp = mergedList;
cout << "Merged Link List is:" << endl;
while (temp) {
cout << temp->data << " ";
temp = temp->next;
}
return 0;
}
C
// C program to merge two sorted linked lists
// using dummy nodes
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* createNode(int new_data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = new_data;
newNode->next = NULL;
return newNode;
}
// Function that takes two lists sorted in increasing order,
// and splices their nodes together to make one big sorted
// list which is returned.
Node* mergeSortedList(Node* first, Node* second) {
// A dummy first node to hang the result on
Node dummy;
dummy.data = -1;
dummy.next = NULL;
// Tail points to the last result node
Node* tail = &dummy;
// So tail->next is the place to add new nodes to the
// result
while (1) {
if (first == NULL) {
// If either list runs out, use the other list
tail->next = second;
break;
}
else if (second == NULL) {
tail->next = first;
break;
}
if (first->data <= second->data) {
// Move the front node from 'first' to the
// result list
Node* newNode = first;
first = first->next;
newNode->next = tail->next;
tail->next = newNode;
}
else {
// Move the front node from 'second' to the
// result list
Node* newNode = second;
second = second->next;
newNode->next = tail->next;
tail->next = newNode;
}
tail = tail->next;
}
return (dummy.next);
}
// Driver code
int main() {
// Create a hard-coded linked list:
// 2 -> 4 -> 8 -> 9
Node* first = createNode(2);
first->next = createNode(4);
first->next->next = createNode(8);
first->next->next->next = createNode(9);
// Create another hard-coded linked list:
// 1 -> 3 -> 8 -> 10
Node* second = createNode(1);
second->next = createNode(3);
second->next->next = createNode(8);
second->next->next->next = createNode(10);
Node* mergedList = mergeSortedList(first, second);
Node* temp = mergedList;
printf("Merged Link List is:\n");
while (temp) {
printf("%d ", temp->data);
temp = temp->next;
}
return 0;
}
Java
// Java program to merge two sorted linked lists
// using dummy nodes
class Node {
int data;
Node next;
Node(int new_data) {
this.data = new_data;
this.next = null;
}
}
class GfG {
// Function that takes two lists sorted in increasing
// order, and splices their nodes together to make one
// big sorted list which is returned.
public static Node mergeSortedList(Node first,
Node second) {
// A dummy first node to hang the result on
Node dummy = new Node(-1);
// Tail points to the last result node
Node tail = dummy;
// So tail.next is the place to add new nodes to the
// result
while (true) {
if (first == null) {
// If either list runs out, use the other
// list
tail.next = second;
break;
}
else if (second == null) {
tail.next = first;
break;
}
if (first.data <= second.data) {
// Move the front node from 'first' to the
// result list
Node newNode = first;
first = first.next;
newNode.next = tail.next;
tail.next = newNode;
}
else {
// Move the front node from 'second' to the
// result list
Node newNode = second;
second = second.next;
newNode.next = tail.next;
tail.next = newNode;
}
tail = tail.next;
}
return (dummy.next);
}
// Driver code
public static void main(String[] args) {
// Create a hard-coded linked list:
// 2 -> 4 -> 8 -> 9
Node first = new Node(2);
first.next = new Node(4);
first.next.next = new Node(8);
first.next.next.next = new Node(9);
// Create another hard-coded linked list:
// 1 -> 3 -> 8 -> 10
Node second = new Node(1);
second.next = new Node(3);
second.next.next = new Node(8);
second.next.next.next = new Node(10);
Node mergedList = mergeSortedList(first, second);
Node temp = mergedList;
System.out.print("Merged Link List is:\n");
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
}
}
Python
# Python program to merge two sorted linked lists
# using dummy nodes
class Node:
def __init__(self, new_data):
self.data = new_data
self.next = None
# Function that takes two lists sorted in increasing order,
# and splices their nodes together to make one big sorted
# list which is returned.
def merge_sorted_list(first, second):
# A dummy first node to hang the result on
dummy = Node(-1)
# Tail points to the last result node
tail = dummy
# So tail.next is the place to add new nodes to the
# result
while True:
if first is None:
# If either list runs out, use the other list
tail.next = second
break
elif second is None:
tail.next = first
break
if first.data <= second.data:
# Move the front node from 'first' to the result
# list
new_node = first
first = first.next
new_node.next = tail.next
tail.next = new_node
else:
# Move the front node from 'second' to the result
# list
new_node = second
second = second.next
new_node.next = tail.next
tail.next = new_node
tail = tail.next
return dummy.next
# Driver code
# Create a hard-coded linked list:
# 2 -> 4 -> 8 -> 9
first = Node(2)
first.next = Node(4)
first.next.next = Node(8)
first.next.next.next = Node(9)
# Create another hard-coded linked list:
# 1 -> 3 -> 8 -> 10
second = Node(1)
second.next = Node(3)
second.next.next = Node(8)
second.next.next.next = Node(10)
merged_list = merge_sorted_list(first, second)
temp = merged_list
print("Merged Link List is:")
while temp:
print(temp.data, end=" ")
temp = temp.next
C#
// C# program to merge two sorted linked lists
// using dummy nodes
using System;
class Node {
public int data;
public Node next;
public Node(int new_data) {
this.data = new_data;
this.next = null;
}
}
class GfG {
// Function that takes two lists sorted in increasing
// order, and splices their nodes together to make one
// big sorted list which is returned.
public static Node mergeSortedList(Node first,
Node second) {
// A dummy first node to hang the result on
Node dummy = new Node(-1);
// Tail points to the last result node
Node tail = dummy;
// So tail.next is the place to add new nodes to the
// result
while (true) {
if (first == null) {
// If either list runs out, use the other
// list
tail.next = second;
break;
}
else if (second == null) {
tail.next = first;
break;
}
if (first.data <= second.data) {
// Move the front node from 'first' to the
// result list
Node newNode = first;
first = first.next;
newNode.next = tail.next;
tail.next = newNode;
}
else {
// Move the front node from 'second' to the
// result list
Node newNode = second;
second = second.next;
newNode.next = tail.next;
tail.next = newNode;
}
tail = tail.next;
}
return dummy.next;
}
// Driver code
public static void Main(string[] args) {
// Create a hard-coded linked list:
// 2 -> 4 -> 8 -> 9
Node first = new Node(2);
first.next = new Node(4);
first.next.next = new Node(8);
first.next.next.next = new Node(9);
// Create another hard-coded linked list:
// 1 -> 3 -> 8 -> 10
Node second = new Node(1);
second.next = new Node(3);
second.next.next = new Node(8);
second.next.next.next = new Node(10);
Node mergedList = mergeSortedList(first, second);
Node temp = mergedList;
Console.Write("Merged Link List is:\n");
while (temp != null) {
Console.Write(temp.data + " ");
temp = temp.next;
}
}
}
JavaScript
// JavaScript program to merge two sorted linked lists
// using dummy nodes
class Node {
constructor(new_data) {
this.data = new_data;
this.next = null;
}
}
class GfG {
// Function that takes two lists sorted in increasing
// order, and splices their nodes together to make one
// big sorted list which is returned.
static mergeSortedList(first, second) {
// A dummy first node to hang the result on
let dummy = new Node(-1);
// Tail points to the last result node
let tail = dummy;
// So tail.next is the place to add new nodes to the
// result
while (true) {
if (first === null) {
// If either list runs out, use the other
// list
tail.next = second;
break;
}
else if (second === null) {
tail.next = first;
break;
}
if (first.data <= second.data) {
// Move the front node from 'first' to the
// result list
let newNode = first;
first = first.next;
newNode.next = tail.next;
tail.next = newNode;
}
else {
// Move the front node from 'second' to the
// result list
let newNode = second;
second = second.next;
newNode.next = tail.next;
tail.next = newNode;
}
tail = tail.next;
}
return dummy.next;
}
}
// Driver code
// Create a hard-coded linked list:
// 2 -> 4 -> 8 -> 9
let first = new Node(2);
first.next = new Node(4);
first.next.next = new Node(8);
first.next.next.next = new Node(9);
// Create another hard-coded linked list:
// 1 -> 3 -> 8 -> 10
let second = new Node(1);
second.next = new Node(3);
second.next.next = new Node(8);
second.next.next.next = new Node(10);
let mergedList = GfG.mergeSortedList(first, second);
let temp = mergedList;
process.stdout.write("Merged Link List is:\n");
while (temp !== null) {
process.stdout.write(temp.data + " ");
temp = temp.next;
}
OutputMerged Linked List is:
2 3 5 10 15 20
Time Complexity: O(N + M), where N and M are the size of list1 and list2 respectively
Auxiliary Space: O(1)
Merge Two Sorted Linked Lists
Merge Two Sorted Linked Lists with O(1) Auxiliary Space
Similar Reads
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
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] U
13 min read
Merge K Sorted Linked Lists using Min Heap
Given k sorted linked lists of different sizes, the task is to merge them all maintaining their sorted order. Examples: Input: K = 3, N = 4list1 = 1->3->5->7->NULLlist2 = 2->4->6->8->NULLlist3 = 0->9->10->11->NULL Output: 0->1->2->3->4->5->6->
9 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
Sorted merge of two sorted doubly circular linked lists
Given two sorted Doubly circular Linked List containing n and m nodes respectively. The task is to merge the two lists such that resultant list is also in sorted order. Example: Input: List 1: List 2: Output: [Naive Approach] Using Array and Sorting - O((n+m) * log(n+m)) time and O(n+m) spaceThe ide
15+ 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
15+ min read
Merge two sorted linked list without duplicates
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->9Output : 1 2 4 5 7 9Source: Microsoft on Campus Placement and Interview Question
15+ 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
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
Javascript Program To Merge K Sorted Linked Lists Using Min Heap - Set 2
Given k linked lists each of size n and each list is sorted in non-decreasing order, merge them into a single sorted (non-decreasing order) linked list and print the sorted linked list as output. Examples: Input: k = 3, n = 4list1 = 1->3->5->7->NULLlist2 = 2->4->6->8->NULLlis
5 min read