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;
   }
}
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();
      while (list1 && list2) {
         if (list1.value <= list2.value) {
            mergedList.add(list1.value);
            list1 = list1.next;
         } else {
            mergedList.add(list2.value);
            list2 = list2.next;
         }
      }
      while (list1) {
         mergedList.add(list1.value);
         list1 = list1.next;
      }
      while (list2) {
         mergedList.add(list2.value);
         list2 = list2.next;
      }
      let current = mergedList.head;
      let previous = null;
      let next = null;
      while (current) {
         next = current.next;
         current.next = previous;
         previous = current;
         current = next;
      }
      mergedList.head = previous;
      return mergedList;
   }
}
// Example
const list1 = new LinkedList();
list1.add(1);
list1.add(3);
list1.add(5);
list1.add(7);
const list2 = new LinkedList();
list2.add(2);
list2.add(4);
list2.add(6);
console.log('Linked List 1:');
list1.printList();
console.log('Linked List 2:');
list2.printList();
const mergedList = new LinkedList();
mergedList.head = mergedList.merge(list1.head, list2.head).head;
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

169 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements