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).
- Merge a linked list into another linked list at alternate positions
- Sum of the alternate nodes of linked list
- Alternate Odd and Even Nodes in a Singly Linked List
- Rearrange a linked list in to alternate first and last element
- Delete alternate nodes of a Linked List
- Subtraction of the alternate nodes of Linked List
- Product of the alternate nodes of 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
- Convert singly linked list into circular linked list
- Difference between Singly linked list and Doubly linked list
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.