Timer is Running

Linked List that is Sorted Alternatingly
Submissions: 6456   Accuracy:


  Difficulty: Easy   Marks: 2
Associated Course(s):   Amazon SDE Test Series

Given a Linked list of size N, the list is in alternating ascending and descending orders. Your task is to complete the function sort() that sorts the given linked list in non-decreasing order.

The function takes a single argument as input the reference pointer to the head of the linked list.
There are T test cases and for each test case the function will be called separately.

For each test case function should return reference pointer to the head of the new linked list.

User Task:
The task is to complete the function sort() which should sort the linked list in non-decreasing order

Note: Complete the task in constanst auxilliary space.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

1 <= T <= 100
1 <= N <= 100
0 <= A[] <= 103


1 9 2 8 3 7
13 99 21 80 50

1 2 3 7 8 9
13 21 50 80 99

Testcase 1:
After sorting the given linked list, the list will be as 1-> 2-> 3-> 7-> 8-> 9.


** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Mridul Gupta
Author: harshitsidhwa

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.