Given a Linked list, rearrange it such that converted list should be of the form a < b > c < d > e < f .. where a, b, c are consecutive data node of linked list and such that the order of linked list is sustained.
For example: In 11 15 20 5 10 we consider only 11 20 5 15 10 because it satisfies the above condition and the order of linked list. 5 20 11 15 10 is not considered as it does not follow the order of linked list.
You have to complete the method which takes 1 argument: the head of the linked list. You should not read any input from stdin/console. There are multiple test cases. For each test case, this method will be called individually.
Your function should return a pointer to the zig zag linked list.
The task is to complete the function zigZag() which should reorder the list as required.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
1 <= T <= 100
1 <= size of linked list(a) <= 1000
1 2 3 4
11 15 20 5 10
1 3 2 4
11 20 5 15 10
Testcase 2: In the given linked list, after arranging them as 11 < 20 > 5 < 15 > 10 in the pattern as asked above.
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.