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 2<->4<->8<->10
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.
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
Thanks to Goku for providing above implementation in a comment here.
Time Complexity: Time complexity of the above implementation is same as time complexity of MergeSort for arrays. It takes Θ(nLogn) time.
You may also like to see QuickSort for doubly linked list
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- How to make Mergesort to perform O(n) comparisons in best case?
- 3-way Merge Sort
- Find pairs with given sum in doubly linked list
- Convert a Binary Tree to a Circular Doubly Link List
- Create a Doubly Linked List from a Ternary Tree
- Insertion Sort for Singly Linked List
- Why Quick Sort preferred for Arrays and Merge Sort for Linked Lists?
- QuickSort on Singly Linked List
- Swap Kth node from beginning with Kth node from end in a Linked List
- QuickSort on Doubly Linked List
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- Merge Sort for Linked Lists
- Delete a node in a Doubly Linked List
- Reverse a Doubly Linked List
- The Great Tree-List Recursion Problem.