Javascript Program For Merge Sort For Doubly Linked List Last Updated : 30 Aug, 2024 Comments Improve Suggest changes Like Article Like Report 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 24810Merge 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 // javascript program to implement merge sort in singly linked list // Linked List Class let 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; console.log("Forward Traversal using next pointer"); while (node != null) { console.log(node.data + " "); temp = node; node = node.next; } console.log("Backward Traversal using prev pointer"); while (temp != null) { console.log(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; } let 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); console.log("Linked list after sorting :"); print(node); // This code is contributed by umadevi9616 OutputLinked 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 Complexity Analysis: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! Comment More infoAdvertise with us Next Article Javascript Program For Merge Sort For Doubly Linked List kartik Follow Improve Article Tags : Linked List Sorting JavaScript Web Technologies DSA Amazon Merge Sort doubly linked list Linked-List-Sorting +5 More Practice Tags : AmazonLinked ListMerge SortSorting Similar Reads Javascript Program For Merge Sort Of Linked Lists Merge sort is often preferred for sorting a linked list. The slow random-access performance of a linked list makes some other algorithms (such as quicksort) perform poorly, and others (such as heapsort) completely impossible. Let the head be the first node of the linked list to be sorted and headRef 6 min read Merge Sort for Linked Lists in JavaScript Prerequisite: Merge Sort for Linked Lists Merge sort is often preferred for sorting a linked list. The slow random-access performance of a linked list makes some other algorithms (such as quicksort) perform poorly, and others (such as heapsort) completely impossible. In this post, Merge sort for lin 4 min read Javascript Program To Merge K Sorted Linked Lists - Set 1 Given K sorted linked lists of size N each, merge them and print the sorted output.Examples: Input: k = 3, n = 4list1 = 1->3->5->7->NULLlist2 = 2->4->6->8->NULLlist3 = 0->9->10->11->NULLOutput: 0->1->2->3->4->5->6->7->8->9->10->11M 6 min read Merge Sort for Doubly Linked List Given a doubly linked list, The task is to sort the doubly linked list in non-decreasing order using merge sort.Examples:Input: 10 <-> 8 <-> 4 <-> 2Output: 2 <-> 4 <-> 8 <-> 10Input: 5 <-> 3 <-> 2Output: 2 <-> 3 <-> 5 Note: Merge sort for a 13 min read Javascript Program For Insertion Sort In A Singly Linked List We have discussed Insertion Sort for arrays. In this article we are going to discuss Insertion Sort for linked list. Below is a simple insertion sort algorithm for a linked list. 1) Create an empty sorted (or result) list. 2) Traverse the given list, do following for every node. ......a) Insert curr 3 min read Javascript Program For Finding Intersection Of Two Sorted Linked Lists Given two lists sorted in increasing order, create and return a new list representing the intersection of the two lists. The new list should be made with its own memory â the original lists should not be changed. Example: Input: First linked list: 1->2->3->4->6Second linked list be 2- 3 min read Javascript Program To Merge K Sorted Linked Lists Using Min Heap - Set 2 Given k linked lists each of size n and each list is sorted in non-decreasing order, merge them into a single sorted (non-decreasing order) linked list and print the sorted linked list as output.Examples: Input: k = 3, n = 4list1 = 1->3->5->7->NULLlist2 = 2->4->6->8->NULLlist 5 min read Merge K sorted Doubly Linked List in Sorted Order Given K sorted doubly linked list. The task is to merge all sorted doubly linked list in single sorted doubly linked list means final list must be sorted.Examples: Input: List 1 : 2 <-> 7 <-> 8 <-> 12 <-> 15 <-> NULL List 2 : 4 <-> 9 <-> 10 <-> NULL Li 15+ min read Insertion Sort for Doubly Linked List Given a doubly linked list, the task is to sort the doubly linked list in non-decreasing order using the insertion sort.Examples:Input: head: 5<->3<->4<->1<->2Output: 1<->2<->3<->4<->5Explanation: Doubly Linked List after sorting using insertion sort t 10 min read Javascript Program For Sorting A Linked List Of 0s, 1s And 2s By Changing Links Given a linked list of 0s, 1s and 2s, sort it.Examples:Input: 2->1->2->1->1->2->0->1->0Output: 0->0->1->1->1->1->2->2->2The sorted Array is 0, 0, 1, 1, 1, 1, 2, 2, 2.Input: 2->1->0Output: 0->1->2The sorted Array is 0, 1, 2Method 1: There is 3 min read Like