# 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. So, let's get started and learn how to write a JavaScript program to merge two sorted linked lists in reverse order.

## Problem Statement

We have given 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 (The expected output after merging the two lists in reverse order)


Note − The resulting merged list should also be sorted in descending order.

Example 2 − Another example with linked lists having 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 (The expected output after merging the two lists in reverse order)

Note − The merged list still contains duplicates in descending order.

Now, let’s try to understand the algorithm for the above problem statement.

## Algorithm for merging two sorted linked lists in reverse order

• Create a Node class that stores a value and a next pointer.

• Create a LinkedList class that stores a head pointer and a size variable.

• Implement an add() method that adds a new node to the end of the list.

• Implement a printList() method that prints the list.

• Implement a merge() method that takes two sorted linked lists as input and returns a new linked list that is sorted in reverse order.

• Create an empty mergedList and two pointers, list1 and list2, that point to the heads of the input lists.

• While both list1 and list2 are not null, compare their values and add the larger value to mergedList.

• If one of the lists becomes null, add the remaining nodes of the other list to mergedList.

• Reverse the order of the nodes in mergedList and return it.

• Create two linked lists and merge them using the merge() method.

• Print the input and output lists.

Now that we understand the algorithm, let's understand its implementation with the help of an example where we implement this algorithm using Javascript.

### Example

The program defines two classes, Node and LinkedList, to represent linked lists. It then defines a merge() function that takes two sorted linked lists and merges them into a new linked list that is sorted in reverse order. The function first compares the values of the nodes in the two lists and adds the greater value to the new list. Once one of the input lists has been exhausted, the function adds the remaining nodes from the other list to the new list. Finally, the function reverses the order of the nodes in the new list and returns it.

class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
constructor() {
this.size = 0;
}
const node = new Node(value);
} else {
while (current.next) {
current = current.next;
}
current.next = node;
}
this.size++;
}
printList() {
let result = '';
while (current) {
result += current.value + ' -> ';
current = current.next;
}
result += 'null';
console.log(result);
}
merge(list1, list2) {
while (list1 && list2) {
if (list1.value <= list2.value) {
list1 = list1.next;
} else {
list2 = list2.next;
}
}
while (list1) {
list1 = list1.next;
}
while (list2) {
list2 = list2.next;
}
let previous = null;
let next = null;
while (current) {
next = current.next;
current.next = previous;
previous = current;
current = next;
}
return mergedList;
}
}
// Example
list1.printList();
list2.printList();
console.log('Merged List:');
mergedList.printList();


## Conclusion

So in this tutorial, we have discussed how to merge two sorted linked lists in JavaScript in such a way that the resulting list is in reverse order. We have provided a program that demonstrates this algorithm by defining two classes, Node and LinkedList, and implementing a merge() method that takes two sorted linked lists as input and returns a new linked list that is sorted in reverse order. By following the algorithm and program outlined in this tutorial, learners can merge two sorted linked lists in JavaScript with ease.

Updated on: 17-Apr-2023

145 Views 