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

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

Input:
First line of input contains number of testcases T. For each testcase, first line of input contains number of nodes in the linked list and next line contains N elements of the linked list.

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

Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to 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

Explanation:
Testcase 1:
After sorting the given linked list, the resultant matrix will be 1->2->3->4->5.

 

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

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.