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 firstname.lastname@example.org. 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
- Bubble Sort On Doubly Linked List
- Insertion Sort for Doubly Linked List
- Merge Sort for Doubly Linked List
- Sort a k sorted 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
- Reverse a Doubly Linked List
- QuickSort on Doubly Linked List
- Reverse a Doubly Linked List | Set-2
- Delete all occurrences of a given key in a doubly linked list
- Rotate Doubly linked list by N nodes
- Doubly Linked List | Set 1 (Introduction and Insertion)
- Doubly Circular Linked List | Set 2 (Deletion)