Java Program For Merge Sort For Doubly Linked List Last Updated : 09 Dec, 2022 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 24810 Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. 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. Java // Java program to implement merge sort in // singly linked list // Linked List Class class LinkedList { // Head of list static Node head; // Node Class static class Node { int data; Node next, prev; // Constructor to create a // new node Node(int d) { data = d; next = prev = null; } } void print(Node node) { Node temp = node; System.out.println( "Forward Traversal using next pointer"); while (node != null) { System.out.print(node.data + " "); temp = node; node = node.next; } System.out.println( "Backward Traversal using prev pointer"); while (temp != null) { System.out.print(temp.data + " "); temp = temp.prev; } } // Split a doubly linked list (DLL) into // 2 DLLs of half sizes Node split(Node head) { Node fast = head, slow = head; while (fast.next != null && fast.next.next != null) { fast = fast.next.next; slow = slow.next; } Node temp = slow.next; slow.next = null; return temp; } Node mergeSort(Node node) { if (node == null || node.next == null) { return node; } Node 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 Node merge(Node first, Node 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 code public static void main(String[] args) { LinkedList list = new LinkedList(); list.head = new Node(10); list.head.next = new Node(30); list.head.next.next = new Node(3); list.head.next.next.next = new Node(4); list.head.next.next.next.next = new Node(20); list.head.next.next.next.next.next = new Node(5); Node node = null; node = list.mergeSort(head); System.out.println( "Linked list after sorting :"); list.print(node); } } // This code is contributed by Mayank Jaiswal 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! Comment More infoAdvertise with us Next Article Java Program For Merge Sort For Doubly Linked List kartik Follow Improve Article Tags : Linked List Sorting Java Programs DSA Amazon Merge Sort doubly linked list Linked-List-Sorting +4 More Practice Tags : AmazonLinked ListMerge SortSorting Similar Reads Java Program For QuickSort On Doubly Linked List Following is a typical recursive implementation of QuickSort for arrays. The implementation uses last element as pivot. Java /* A typical recursive implementation of Quicksort for array*/ /* This function takes last element as pivot, places the pivot element at its correct position in sorted array, 5 min read Java Program For Reversing A Doubly Linked List Given a Doubly Linked List, the task is to reverse the given Doubly Linked List. See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked List Here is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes, 5 min read Java 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 = 4 list1 = 1->3->5->7->NULL list2 = 2->4->6->8->NULL list3 = 0->9->10->11->NULL Output: 0->1->2->3->4->5->6->7->8->9->10->11 Merged lists in a sorted order where every element is greater t 6 min read Java Program For Deleting A Node In A Doubly Linked List Pre-requisite: Doubly Link List Set 1| Introduction and Insertion Write a function to delete a given node in a doubly-linked list. Original Doubly Linked List Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Approach: The deletion of a node in a doubly-linked list 4 min read Java 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 4 min read Java Program For QuickSort On Singly Linked List QuickSort on Doubly Linked List is discussed here. QuickSort on Singly linked list was given as an exercise. The important things about implementation are, it changes pointers rather swapping data and time complexity is same as the implementation for Doubly Linked List. Recommended: Please solve it 3 min read Java Program For Flattening A Linked List Given a linked list where every node represents a linked list and contains two pointers of its type: Pointer to next node in the main list (we call it 'right' pointer in the code below).Pointer to a linked list where this node is headed (we call it the 'down' pointer in the code below). All linked l 4 min read Java Program for Merge 3 Sorted Arrays Given 3 arrays (A, B, C) which are sorted in ascending order, we are required to merge them together in ascending order and output the array D. Examples: Input : A = [1, 2, 3, 4, 5] B = [2, 3, 4] C = [4, 5, 6, 7] Output : D = [1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 6, 7] Input : A = [1, 2, 3, 5] B = [6, 7, 8 7 min read Java Program to Merge Two Sorted Linked Lists in New List We are given two sorted List and our goal is to merge these two lists into a new list. For that, we have to write one function which will take two List as an argument which is sorted in increasing order. This function will Merge these two List into one List in increasing order. Input List 1 : 1-> 4 min read Java 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->6 Second linked list be 2- 5 min read Like