Given Pointer/Reference to the head of a linked list, task is to Sort the given linked list using Merge Sort.

You need to complete the function splitList() and mergeList(), which will be called by the function mergeSort().

Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.

Input:
There will be multiple test cases, for each test case function mergeSort() will be called separately. The only input to the function is the pointer/reference to the head of the linked list.

Output:
For each test, in a new line, print the sorted linked list.

Constraints:
1 <= T <= 100
1 <= N <= 105

Example:
Input:

2
5
3 5 2 4 1
3
9 15 0

Ouput:
1 2 3 4 5
0 9 15

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

Author: harshitsidhwa


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

to report an issue on this page.