Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Javascript Program For Merging Two Sorted Linked Lists Such That Merged List Is In Reverse Order

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Given two linked lists sorted in increasing order. Merge them such a way that the result list is in decreasing order (reverse order).

Examples: 

Input:  a: 5->10->15->40
        b: 2->3->20 
Output: res: 40->20->15->10->5->3->2

Input:  a: NULL
        b: 2->3->20 
Output: res: 20->3->2

A Simple Solution is to do following. 
1) Reverse first list ‘a’
2) Reverse second list ‘b’
3) Merge two reversed lists.
Another Simple Solution is first Merge both lists, then reverse the merged list.
Both of the above solutions require two traversals of linked list. 

How to solve without reverse, O(1) auxiliary space (in-place) and only one traversal of both lists? 
The idea is to follow merge style process. Initialize result list as empty. Traverse both lists from beginning to end. Compare current nodes of both lists and insert smaller of two at the beginning of the result list. 

1) Initialize result list as empty: res = NULL.
2) Let 'a' and 'b' be heads first and second lists respectively.
3) While (a != NULL and b != NULL)
    a) Find the smaller of two (Current 'a' and 'b')
    b) Insert the smaller value node at the front of result.
    c) Move ahead in the list of smaller node. 
4) If 'b' becomes NULL before 'a', insert all nodes of 'a' 
   into result list at the beginning.
5) If 'a' becomes NULL before 'b', insert all nodes of 'a' 
   into result list at the beginning. 

Below is the implementation of above solution.

Javascript




<script>
// Javascript program to merge two
// sorted linked list such that merged
// list is in reverse order
 
// Node Class
class Node
{
    constructor(d)
    {
        this.data = d;
        this.next = null;
    }
}
 
// Head of list
let head; 
let a, b;
 
function printlist(node)
{
    while (node != null)
    {
        document.write(node.data + " ");
        node = node.next;
    }
}
 
function sortedmerge(node1, node2)
{   
    // If both the nodes are null
    if (node1 == null && node2 == null)
    {
        return null;
    }
 
    // Resultant node
    let res = null;
 
    // If both of them have nodes
    //  present traverse them
    while (node1 != null &&
           node2 != null)
    {       
        // Now compare both nodes
        // current data
        if (node1.data <= node2.data)
        {
            let temp = node1.next;
            node1.next = res;
            res = node1;
            node1 = temp;
        }
        else
        {
            let temp = node2.next;
            node2.next = res;
            res = node2;
            node2 = temp;
        }
    }
 
    // If second list reached end, but
    // first list has nodes. Add
    // remaining nodes of first
    // list at the front of result list
    while (node1 != null)
    {
        let temp = node1.next;
        node1.next = res;
        res = node1;
        node1 = temp;
    }
 
    // If first list reached end, but
    // second list has node. Add
    // remaining nodes of first
    // list at the front of result list
    while (node2 != null)
    {
        let temp = node2.next;
        node2.next = res;
        res = node2;
        node2 = temp;
    }
    return res;
}
 
// Driver code
let result = null;
   
/*Let us create two sorted linked lists
  to test the above functions. Created
  lists shall be
  a: 5->10->15
  b: 2->3->20*/
a = new Node(5);
a.next = new Node(10);
a.next.next = new Node(15);
 
b = new Node(2);
b.next = new Node(3);
b.next.next = new Node(20);
 
document.write("List a before merge :<br>");
printlist(a);
document.write("<br>");
document.write("List b before merge :<br>");
printlist(b);
 
// Merge two sorted linkedlist in
// decreasing order
result = sortedmerge(a, b);
document.write("<br>");
document.write("Merged linked list : <br>");
printlist(result);
// This code is contributed by rag2127
</script>


Output: 

List A before merge: 
5 10 15 
List B before merge: 
2 3 20 
Merged Linked List is: 
20 15 10 5 3 2 

Time Complexity: O(N)

Auxiliary Space: O(1)

This solution traverses both lists only once, doesn’t require reverse and works in-place.
This article is contributed by Mohammed Raqeeb. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
 

Please refer complete article on Merge two sorted linked lists such that merged list is in reverse order for more details!


My Personal Notes arrow_drop_up
Last Updated : 22 Mar, 2022
Like Article
Save Article
Similar Reads
Related Tutorials