Given the heads of two linked lists head1 and head2, where both linked lists are sorted in non-decreasing order, merge them into a single linked list such that the resulting linked list is sorted in non-increasing order.
Examples:
Input: head1 = 1->3, head2 = 2->4
Output: 4->3->2->1
Explanation: After merging the two lists in non-increasing order, we have new lists as 4->3->2->1.Input: head1 = 5->10->15->40, head2 = 2->3->20
Output: 40->20->15->10->5->3->2
Explanation: After merging the two lists in non-increasing order, we have new lists as 40->20->15->10->5->3->2.
Try It Yourself
Merge with Insertion at Front - O(m + n) Time and O(1) Space
Traverse both sorted lists from beginning to end. Insert each node at the front of result list. This builds the merged list in reverse sorted order.
- Initialize result head as null.
- While both lists have nodes, compare their data
- Take the smaller node and insert at front of result list
- Move pointer of selected list forward
- When one list becomes empty, insert remaining nodes of other list at front
#include <iostream>
using namespace std;
/* Structure of a linked list Node */
class Node
{
public:
int data;
Node *next;
Node(int val)
{
data = val;
next = nullptr;
}
};
Node *mergeResult(Node *head1, Node *head2)
{
// If both lists are empty
if (head1 == nullptr && head2 == nullptr)
return nullptr;
// Initialize head of resultant list
Node *res = nullptr;
// Traverse both lists while both have nodes
while (head1 != nullptr && head2 != nullptr)
{
// If current value in first list is smaller
if (head1->data <= head2->data)
{
// Store next node
Node *temp = head1->next;
// Add current node at front of result
head1->next = res;
res = head1;
// Move ahead in first list
head1 = temp;
}
else
{
// Store next node
Node *temp = head2->next;
// Add current node at front of result
head2->next = res;
res = head2;
// Move ahead in second list
head2 = temp;
}
}
// Add remaining nodes of first list
while (head1 != nullptr)
{
Node *temp = head1->next;
head1->next = res;
res = head1;
head1 = temp;
}
// Add remaining nodes of second list
while (head2 != nullptr)
{
Node *temp = head2->next;
head2->next = res;
res = head2;
head2 = temp;
}
return res;
}
// Function to print linked list
void printList(Node *head)
{
while (head != nullptr)
{
cout << head->data << " ";
head = head->next;
}
cout << endl;
}
int main()
{
// First sorted list: 5 -> 10 -> 15
Node *head1 = new Node(5);
head1->next = new Node(10);
head1->next->next = new Node(15);
// Second sorted list: 2 -> 3 -> 20
Node *head2 = new Node(2);
head2->next = new Node(3);
head2->next->next = new Node(20);
cout << "List 1: ";
printList(head1);
cout << "List 2: ";
printList(head2);
Node *result = mergeResult(head1, head2);
cout << "Merged List in Reverse Order: ";
printList(result);
return 0;
}
// Java program to merge two sorted linked lists in reverse order
import java.util.*;
class GfG {
// Node class defined inside GfG
static class Node {
int data;
Node next;
Node(int val) {
data = val;
next = null;
}
}
static Node mergeResult(Node head1, Node head2) {
// If both lists are empty
if (head1 == null && head2 == null)
return null;
// Initialize head of resultant list
Node res = null;
// Traverse both lists while both have nodes
while (head1 != null && head2 != null) {
// If current value in first list is smaller
if (head1.data <= head2.data) {
// Store next node
Node temp = head1.next;
// Add current node at front of result
head1.next = res;
res = head1;
// Move ahead in first list
head1 = temp;
} else {
// Store next node
Node temp = head2.next;
// Add current node at front of result
head2.next = res;
res = head2;
// Move ahead in second list
head2 = temp;
}
}
// Add remaining nodes of first list
while (head1 != null) {
Node temp = head1.next;
head1.next = res;
res = head1;
head1 = temp;
}
// Add remaining nodes of second list
while (head2 != null) {
Node temp = head2.next;
head2.next = res;
res = head2;
head2 = temp;
}
return res;
}
// Function to print linked list
static void printList(Node head) {
while (head != null) {
System.out.print(head.data + " ");
head = head.next;
}
System.out.println();
}
public static void main(String[] args) {
// First sorted list: 5 -> 10 -> 15
Node head1 = new Node(5);
head1.next = new Node(10);
head1.next.next = new Node(15);
// Second sorted list: 2 -> 3 -> 20
Node head2 = new Node(2);
head2.next = new Node(3);
head2.next.next = new Node(20);
System.out.print("List 1: ");
printList(head1);
System.out.print("List 2: ");
printList(head2);
Node result = mergeResult(head1, head2);
System.out.print("Merged List in Reverse Order: ");
printList(result);
}
}
# Python program to merge two sorted linked lists in reverse order
class Node:
def __init__(self, val):
self.data = val
self.next = None
def mergeResult(head1, head2):
# If both lists are empty
if head1 is None and head2 is None:
return None
# Initialize head of resultant list
res = None
# Traverse both lists while both have nodes
while head1 is not None and head2 is not None:
# If current value in first list is smaller
if head1.data <= head2.data:
# Store next node
temp = head1.next
# Add current node at front of result
head1.next = res
res = head1
# Move ahead in first list
head1 = temp
else:
# Store next node
temp = head2.next
# Add current node at front of result
head2.next = res
res = head2
# Move ahead in second list
head2 = temp
# Add remaining nodes of first list
while head1 is not None:
temp = head1.next
head1.next = res
res = head1
head1 = temp
# Add remaining nodes of second list
while head2 is not None:
temp = head2.next
head2.next = res
res = head2
head2 = temp
return res
# Function to print linked list
def printList(head):
while head is not None:
print(head.data, end=" ")
head = head.next
print()
# Driver code
if __name__ == "__main__":
# First sorted list: 5 -> 10 -> 15
head1 = Node(5)
head1.next = Node(10)
head1.next.next = Node(15)
# Second sorted list: 2 -> 3 -> 20
head2 = Node(2)
head2.next = Node(3)
head2.next.next = Node(20)
print("List 1: ", end="")
printList(head1)
print("List 2: ", end="")
printList(head2)
result = mergeResult(head1, head2)
print("Merged List in Reverse Order: ", end="")
printList(result)
// C# program to merge two sorted linked lists in reverse order
using System;
class Node {
public int data;
public Node next;
public Node(int val) {
data = val;
next = null;
}
}
class GfG {
static Node mergeResult(Node head1, Node head2) {
// If both lists are empty
if (head1 == null && head2 == null)
return null;
// Initialize head of resultant list
Node res = null;
// Traverse both lists while both have nodes
while (head1 != null && head2 != null) {
// If current value in first list is smaller
if (head1.data <= head2.data) {
// Store next node
Node temp = head1.next;
// Add current node at front of result
head1.next = res;
res = head1;
// Move ahead in first list
head1 = temp;
} else {
// Store next node
Node temp = head2.next;
// Add current node at front of result
head2.next = res;
res = head2;
// Move ahead in second list
head2 = temp;
}
}
// Add remaining nodes of first list
while (head1 != null) {
Node temp = head1.next;
head1.next = res;
res = head1;
head1 = temp;
}
// Add remaining nodes of second list
while (head2 != null) {
Node temp = head2.next;
head2.next = res;
res = head2;
head2 = temp;
}
return res;
}
// Function to print linked list
static void printList(Node head) {
while (head != null) {
Console.Write(head.data + " ");
head = head.next;
}
Console.WriteLine();
}
static void Main(string[] args) {
// First sorted list: 5 -> 10 -> 15
Node head1 = new Node(5);
head1.next = new Node(10);
head1.next.next = new Node(15);
// Second sorted list: 2 -> 3 -> 20
Node head2 = new Node(2);
head2.next = new Node(3);
head2.next.next = new Node(20);
Console.Write("List 1: ");
printList(head1);
Console.Write("List 2: ");
printList(head2);
Node result = mergeResult(head1, head2);
Console.Write("Merged List in Reverse Order: ");
printList(result);
}
}
// JavaScript program to merge two sorted linked lists in reverse order
class Node {
constructor(val) {
this.data = val;
this.next = null;
}
}
function mergeResult(head1, head2) {
// If both lists are empty
if (head1 === null && head2 === null)
return null;
// Initialize head of resultant list
let res = null;
// Traverse both lists while both have nodes
while (head1 !== null && head2 !== null) {
// If current value in first list is smaller
if (head1.data <= head2.data) {
// Store next node
let temp = head1.next;
// Add current node at front of result
head1.next = res;
res = head1;
// Move ahead in first list
head1 = temp;
} else {
// Store next node
let temp = head2.next;
// Add current node at front of result
head2.next = res;
res = head2;
// Move ahead in second list
head2 = temp;
}
}
// Add remaining nodes of first list
while (head1 !== null) {
let temp = head1.next;
head1.next = res;
res = head1;
head1 = temp;
}
// Add remaining nodes of second list
while (head2 !== null) {
let temp = head2.next;
head2.next = res;
res = head2;
head2 = temp;
}
return res;
}
// Function to print linked list
function printList(head) {
let result = [];
while (head !== null) {
result.push(head.data);
head = head.next;
}
console.log(result.join(' '));
}
// Driver code
// First sorted list: 5 -> 10 -> 15
let head1 = new Node(5);
head1.next = new Node(10);
head1.next.next = new Node(15);
// Second sorted list: 2 -> 3 -> 20
let head2 = new Node(2);
head2.next = new Node(3);
head2.next.next = new Node(20);
console.log("List 1: ");
printList(head1);
console.log("List 2: ");
printList(head2);
let result = mergeResult(head1, head2);
console.log("Merged List in Reverse Order: ");
printList(result);
Output
List 1: 5 10 15 List 2: 2 3 20 Merged List in Reverse Order: 20 15 10 5 3 2

