Problem of the day
Given a linked list of 'N' nodes, which has nodes in alternating non-decreasing and non-increasing. Sort the list in non-decreasing order.
For Example:
If given linked list is 1->9->3->8->4 then it should be modified to 1->3->4->8->9.
The first line contains an integer 'N', the size of the linked list.
The second line contains 'N' space-separated integers in alternating ascending and descending orders.
The output contains all the integers in non-decreasing order.
You are not required to print the output, it has already been taken care of. Just implement the function.
7
1 9 2 8 3 7 4
1 2 3 4 7 8 9
Since the given list is 1-> 9-> 2-> 8 -> 3 -> 7-> 4.
After sorting, it will be 1-> 2-> 3-> 4-> 7-> 8-> 9.
6
7 15 8 14 9 13
7 8 9 13 14 15
Since the given list is 7-> 15-> 8-> 14 -> 9 -> 13.
After sorting, it will be 7-> 8-> 9-> 13-> 14-> 15.
Try to solve this problem in O(1) space complexity.
1 <= N <= 10^3
0 <= data <= 10^3
Where 'N' is the length of the linked list.
Time Limit: 1 sec