Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program for Merging Two Sorted Linked Lists Such That Merged List Is in Reverse Order
In this tutorial, we will learn about the JavaScript Program for merging two sorted linked lists such that the merged list is in reverse order. We will first understand the problem statement with some examples. Then we will go through a step-by-step approach to solve this problem, including creating a function that takes in two linked lists as arguments and returns the merged list in reverse order. We will also discuss different approaches and their time complexity to help you choose the most efficient solution.
Problem Statement
We have two linked lists sorted in increasing order and our task is to merge them in such a way that the result list is in decreasing order (reverse order). Let's understand this with some examples.
Example 1
Input
Linked List 1: 1 -> 3 -> 5 -> 7 -> null Linked List 2: 2 -> 4 -> 6 -> null
Output
Merged List: 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> null
Example 2 - Another example with duplicate values:
Input
Linked List 1: 1 -> 3 -> 3 -> 5 -> null Linked List 2: 2 -> 4 -> 6 -> null
Output
Merged List: 6 -> 5 -> 4 -> 3 -> 3 -> 2 -> 1 -> null
Algorithm
The approach involves merging the lists in ascending order first, then reversing the result:
- Create a Node class that stores a value and a next pointer
- Create a LinkedList class with head pointer and utility methods
- Compare values from both lists and merge them in ascending order
- Add remaining nodes from the non-empty list
- Reverse the merged list to get descending order
Implementation
The program defines Node and LinkedList classes. The merge function compares values from both lists, creates a merged list in ascending order, then reverses it for the final result.
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
this.size = 0;
}
add(value) {
const node = new Node(value);
if (!this.head) {
this.head = node;
} else {
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = node;
}
this.size++;
}
printList() {
let current = this.head;
let result = '';
while (current) {
result += current.value + ' -> ';
current = current.next;
}
result += 'null';
console.log(result);
}
merge(list1, list2) {
let mergedList = new LinkedList();
// Merge in ascending order
while (list1 && list2) {
if (list1.value
Linked List 1:
1 -> 3 -> 5 -> 7 -> null
Linked List 2:
2 -> 4 -> 6 -> null
Merged List in Reverse Order:
7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> null
Time and Space Complexity
Time Complexity: O(n + m), where n and m are the lengths of the input lists. We traverse each list once during merging and once during reversal.
Space Complexity: O(n + m) for creating the new merged list. If we modify input lists in-place, it could be reduced to O(1).
Alternative Approach: Direct Insertion
Instead of merging in ascending order and then reversing, we can insert nodes at the beginning to achieve reverse order directly:
function mergeInReverse(list1, list2) {
let result = null;
while (list1 && list2) {
let nodeToAdd;
if (list1.value
Conclusion
We have successfully implemented a JavaScript program to merge two sorted linked lists in reverse order. The solution uses a two-step approach: first merging in ascending order, then reversing the result. This approach has O(n + m) time complexity and is efficient for practical applications involving sorted linked lists.
