Skip to content
Related Articles

Related Articles

Javascript Program For Merge Sort For Doubly Linked List

Improve Article
Save Article
Like Article
  • Last Updated : 28 Dec, 2021

Given a doubly linked list, write a function to sort the doubly linked list in increasing order using merge sort.
For example, the following doubly linked list should be changed to 24810

Merge sort for singly linked list is already discussed. The important change here is to modify the previous pointers also when merging two lists.

Below is the implementation of merge sort for doubly linked list.  

Javascript




<script>
// javascript program to implement merge sort in singly linked list
  
// Linked List Class
  
     var head ; // head of list
  
    /* Node Class */
     class Node {
  
        // Constructor to create a new node
        constructor(d) {
            this.data = d;
            this.next = this.prev = null;
        }
    }
  
    function print( node) {
         temp = node;
        document.write("Forward Traversal using next pointer<br/>");
        while (node != null) {
            document.write(node.data + " ");
            temp = node;
            node = node.next;
        }
        document.write("<br/>Backward Traversal using prev pointer<br/>");
        while (temp != null) {
            document.write(temp.data + " ");
            temp = temp.prev;
        }
    }
  
    // Split a doubly linked list (DLL) into 2 DLLs of
    // half sizes
    function split( head) {
         fast = head, slow = head;
        while (fast.next != null && fast.next.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
         temp = slow.next;
        slow.next = null;
        return temp;
    }
  
    function mergeSort( node) {
        if (node == null || node.next == null) {
            return node;
        }
        var  second = split(node);
  
        // Recur for left and right halves
        node = mergeSort(node);
        second = mergeSort(second);
  
        // Merge the two sorted halves
        return merge(node, second);
    }
  
    // Function to merge two linked lists
    function merge( first,  second) {
        // If first linked list is empty
        if (first == null) {
            return second;
        }
  
        // If second linked list is empty
        if (second == null) {
            return first;
        }
  
        // Pick the smaller value
        if (first.data < second.data) {
            first.next = merge(first.next, second);
            first.next.prev = first;
            first.prev = null;
            return first;
        } else {
            second.next = merge(first, second.next);
            second.next.prev = second;
            second.prev = null;
            return second;
        }
    }
  
    // Driver program to test above functions
        head = new Node(10);
        head.next = new Node(30);
        head.next.next = new Node(3);
        head.next.next.next = new Node(4);
        head.next.next.next.next = new Node(20);
        head.next.next.next.next.next = new Node(5);
  
         node = null;
        node = mergeSort(head);
        document.write("Linked list after sorting :<br/>");
        print(node);
  
// This code is contributed by umadevi9616
</script>

Output: 

Linked List after sorting
Forward Traversal using next pointer
3 4 5 10 20 30
Backward Traversal using prev pointer
30 20 10 5 4 3

Time Complexity: Time complexity of the above implementation is same as time complexity of MergeSort for arrays. It takes Θ(nLogn) time. 

Space Complexity:O(1). We are only using constant amount of extra space.
You may also like to see QuickSort for doubly linked list
Please refer complete article on Merge Sort for Doubly Linked List for more details!


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!