You are given a Singly Linked List of distinct integers. In addition to the ‘next’ pointer, every node of the linked list has a ‘nextHigher’ pointer that currently points to NULL. The task is to make the ‘nextHigher’ pointer of every node point to the next higher value node in the linked list.
A singly linked list is a type of linked list that is unidirectional; that is, it can be traversed in only one direction from head to the last node (tail).
Note:For a node, if there is no higher value node in the linked list, it’s ‘nextHigher’ pointer should point to NULL.
The first line of input contains an integer 'T', the number of test cases to run.
The first and the only line of every test case contains the elements of the singly linked list separated by a single space and terminated by -1. Hence, -1 would never be a list element.
Output Format:
For every test case, print the elements of the modified linked list in ascending order. The elements should be single-space separated, terminated by -1.
The output of each test case is printed in a separate line.
Note
You do not need to print anything, It already has been taken care of. Just implement the function and return the pointer to the minimum node in the modified linked list.
The linked list will be traversed using the ‘nextHigher’ pointer to produce the output.
1 <= T <= 10
1 <= N <= 10^4
-10^3 <= data <= 10^3 and data != -1
Where ‘data’ denotes the value of the nodes in the linked list.
1
6 10 7 8 5 -1
5 6 7 8 10 -1
The ‘nextHigher’ pointer of every node should point to the next higher value in the linked list.

The linked list printed after the above process is 5 6 7 8 10 -1. Note that list is printed starting from the minimum valued node using ‘nextHigher’ pointer.
3
5 10 -6 4 7 -1
10 20 30 40 50 -1
-10 -20 -30 -40 -50 -1
-6 4 6 7 10 -1
10 20 30 40 50 -1
-50 -40 -30 -20 -10 -1
For every node, find the next greater valued node in the linked list.
The idea is very simple. We will consider every node and check all the remaining nodes. If we encounter a node with just greater value than the current node, we will simply connect the ‘NEXT_HIGHER’ pointer of the current node to that node.
Algorithm:
O(N * N), where ‘N’ is the number of nodes in the linked list.
For every node of the linked list, we are traversing all the remaining nodes. The total operations performed will be (N * N). Thus, the final time complexity is O(N * N).
O(1)
Constant additional space is required.