Given a linked list and a value x, partition it such that all nodes less than x come first, then all nodes with value equal to x and finally nodes with value greater than or equal to x. The original relative order of the nodes in each of the three partitions should be preserved. The partition must work in-place.
Input : 1->4->3->2->5->2->3, x = 3 Output: 1->2->2->3->3->4->5 Input : 1->4->2->10 x = 3 Output: 1->2->4->10 Input : 10->4->20->10->3 x = 3 Output: 3->10->4->20->10
To solve this problem we can use partition method of Quick Sort but this would not preserve the original relative order of the nodes in each of the two partitions.
Below is the algorithm to solve this problem :
- Initialize first and last nodes of below three linked lists as NULL.
- Linked list of values smaller than x.
- Linked list of values equal to x.
- Linked list of values greater than x.
- Now iterate through the original linked list. If a node’s value is less than x then append it at the end of smaller list. If the value is equal to x, then at the end of equal list. And if value is greater, then at the end of greater list.
- Now concatenate three lists.
Below is the implementation of above idea.
2 10 4 5 30 50
This article is contributed by Shashank Mishra ( Gullu ). 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Partitioning a linked list around a given value and If we don't care about making the elements of the list "stable"
- Create new linked list from two given linked list with greater element at each node
- Difference between Singly linked list and 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
- Merge a linked list into another linked list at alternate positions
- Check if a linked list is Circular Linked List
- Convert singly linked list into circular linked list
- Convert Singly Linked List to XOR Linked List
- Count triplets in a sorted doubly linked list whose sum is equal to a given value x
- Delete all the nodes from the doubly linked list that are greater than a given value
- Delete all the nodes from a doubly linked list that are smaller than a given value
- Count triplets in a sorted doubly linked list whose product is equal to a given value x
- Merge two sorted linked lists such that merged list is in reverse order
- Sort the linked list in the order of elements appearing in the array
- Print Doubly Linked list in Reverse Order
- Print the last k nodes of the linked list in reverse order | Recursive approach
- Print the last k nodes of the linked list in reverse order | Iterative Approaches
- Merge K sorted Doubly Linked List in Sorted Order
- Traverse Linked List from middle to left-right order using recursion