Given an unsorted Linked List of integers. The task is to sort the Linked List into a wave like Line. A Linked List is said to be sorted in Wave Form if the list after sorting is in the form:
list >= list <= list >= …..
Where list[i] denotes the data at i-th node of the Linked List.
Input : List = 2 -> 4 -> 6 -> 8 -> 10 -> 20 Output : 4 -> 2 -> 8 -> 6 -> 20 -> 10 Input : List = 3 -> 6 -> 5 -> 10 -> 7 -> 20 Output : 6 -> 3 -> 10 -> 5 -> 20 -> 7
A Simple Solution is to use sorting. First sort the input linked list, then swap all adjacent elements.
For example, let the input list be 3 -> 6 -> 5 -> 10 -> 7 -> 20. After sorting, we get 3 -> 5 -> 6 -> 7 -> 10 -> 20. After swapping adjacent elements, we get 5 -> 3 -> 7 -> 6 -> 20 -> 10 which is the required list in wave form.
Time Complexity: O(N*logN), where N is the number nodes in the list.
Efficient Solution: This can be done in O(n) time by doing a single traversal of the given list. The idea is based on the fact that if we make sure that all even positioned (at index 0, 2, 4, ..) elements are greater than their adjacent odd elements, we don’t need to worry about oddly positioned element. Following are simple steps.
Note: Assuming indexes in list starts from zero. That is, list represents the first elements of the linked list.
Traverse all even positioned elements of input linked list, and do following.
- If current element is smaller than previous odd element, swap previous and current.
- If current element is smaller than next odd element, swap next and current.
Below is the implementation of above approach:
90 10 49 1 5 2 23
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.
- 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
- Difference between Singly linked list and Doubly linked list
- Convert Singly Linked List to XOR Linked List
- Create new linked list from two given linked list with greater element at each node
- Why Quick Sort preferred for Arrays and Merge Sort for Linked Lists?
- Sort a linked list of 0s, 1s and 2s
- Merge Sort for Doubly Linked List
- Sort a linked list that is sorted alternating ascending and descending orders?
- Sort linked list which is already sorted on absolute values
- C Program for Bubble Sort on Linked List
- Insertion Sort for Singly Linked List
- Insertion Sort for Doubly Linked List
- Sort the biotonic doubly linked list
- Sort a k sorted doubly linked list
- Recursive selection sort for singly linked list | Swapping node links
- Sort a linked list of 0s, 1s and 2s by changing links
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.