Problem of the day
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘2*T’ lines represent the ‘T’ test cases.
The first and only line of each test case contains integers denoting the nodes of the linked list. Each line is guaranteed to have -1 at the end to signify the end of the linked list.
For each test case, return a pointer pointing to the node which is at the middle of the linked list. If no midpoint exists, return a null pointer.
1.You do not need to print anything, it has already been taken care of. Just implement the given function.
2.For a linked list of size 1, the head node is the midpoint.
3.If no midpoint exists, return a null pointer.
1 <= T <= 50
1 <= N <= 4*10^4
-10^9 <= data <= 10^9
data ≠ -1
Where 'N' is the number of nodes and 'data' is the value of nodes.
Time Limit: 1 sec
2
1 2 3 4 5 -1
4 0 32 5 48 6 -1
3
5
For the first test case:
The linked List is 1->2->3->4->5->NULL
We can clearly see that there are 5 elements in the linked list and the middle element is 3 hence we return a pointer poiniting to the middle element i.e 3.
For the second test case:
The linked List is : 4->0->32->5->48->6->NULL
We have 6 elements in the linked list. we will have 2 middle elements i.e 32 and 5. Since it is specified to return the element farther from the root node, we return 5.
2
2 69 7 -1
1 4 5 8 9 6 3 -1
69
8