Sort the given biotonic doubly linked list. A biotonic doubly linked list is a doubly linked list which is first increasing and then decreasing. A strictly increasing or a strictly decreasing list is also a biotonic doubly linked list.
Approach: Find the first node in the list which is smaller than its previous node. Let it be current. If no such node is present then list is already sorted. Else split the list into two lists, first starting from head node till the current’s previous node and second starting from current node till the end of the list. Reverse the second doubly linked list. Refer this post. Now merge the first and second sorted doubly linked list. Refer merge procedure of this post. The final merged list is the required sorted doubly linked list.
Original Doubly linked list: 2 5 7 12 10 6 4 1 Doubly linked list after sorting: 1 2 4 5 6 7 10 12
Time Complexity: O(n)
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Sort the biotonic doubly linked list | Set-2
- Insertion Sort for Doubly Linked List
- Sort a k sorted doubly linked list
- Merge Sort for Doubly Linked List
- Bubble Sort On Doubly Linked List
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Difference between Singly linked list and Doubly linked list
- Reverse a Doubly Linked List | Set-2
- Reverse a Doubly Linked List
- QuickSort on Doubly Linked List
- Delete all the even nodes from a Doubly Linked List
- Delete all occurrences of a given key in a doubly linked list
- Find pairs with given sum in doubly linked list
- Doubly Circular Linked List | Set 2 (Deletion)