Given a linked list containing n nodes. The problem is to rearrange the nodes of the list in such a way that the data in first node is first minimum, second node is first maximum, third node is second minimum, fourth node is second maximum and so on.
Input : list: 4->1->3->5->2 Output : 1->5->2->4->3 Input : list: 10->9->8->7->6->5 Output : 5->10->6->9->7->8
Approach: Following are the steps:
- Sort the linked list using Merge Sort technique.
- Split the list into front and back lists. Refer FrontBackProcedure of this post.
- Now, reverse the back list. Refer this post.
- Finally, merge the nodes of first and back lists in alternate order.
Original list: 10 9 8 7 6 5 Modified list: 5 10 6 9 7 8
Time Complexity: O(n*logn).
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
- Merge a linked list into another linked list at alternate positions
- Sum of the alternate nodes of linked list
- Subtraction of the alternate nodes of Linked List
- Rearrange a linked list in to alternate first and last element
- Product of the alternate nodes of linked list
- Alternate Odd and Even Nodes in a Singly Linked List
- Delete alternate nodes of a Linked List
- Print alternate nodes of a linked list using recursion
- Given a linked list, reverse alternate nodes and append at the end
- Reverse alternate K nodes in a Singly Linked List
- Print the alternate nodes of linked list (Iterative Method)
- Reverse alternate K nodes in a Singly Linked List - Iterative Solution
- Create new linked list from two given linked list with greater element at each node
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.