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.
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
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Sort the biotonic doubly linked list | Set-2
- Insertion Sort for Doubly Linked List
- Sort the biotonic doubly linked list
- Sort a k sorted doubly linked list
- Bubble Sort On Doubly Linked List
- Iterative Merge Sort for Linked List
- Merge K sorted Doubly Linked List in Sorted Order
- Why Quick Sort preferred for Arrays and Merge Sort for Linked Lists?
- Difference between Singly linked list and Doubly linked list
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- Merge a linked list into another linked list at alternate positions
- Merge Sort for Linked Lists
- Construct a Doubly linked linked list from 2D Matrix
- Difference of two Linked Lists using Merge sort
- Union and Intersection of two linked lists | Set-2 (Using Merge Sort)
- Sorted merge of two sorted doubly circular linked lists
- Reverse a Doubly Linked List | Set-2
- Doubly Linked List Tutorial