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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Sort the biotonic doubly linked list | Set-2
- Merge Sort for Doubly Linked List
- Sort a k sorted doubly linked list
- Insertion Sort for Doubly Linked List
- Bubble Sort On Doubly Linked List
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Difference between Singly linked list and Doubly linked list
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- Construct a Doubly linked linked list from 2D Matrix
- QuickSort on Doubly Linked List
- Doubly Linked List Tutorial
- Reverse a Doubly Linked List
- Reverse a Doubly Linked List | Set-2
- Find pairs with given sum in doubly linked list
- Implementation of Deque using doubly linked list