Update appNew update is available. Click here to update.

Modify Linked list

Posted: 16 Jan, 2021
Difficulty: Easy


Try Problem

You are given a linked list containing N nodes where each node is associated with a certain value. Return a linked list as follows: If the input linked list is 1-->2-->3-->4, then the output should be 1-->4-->2-->3. And if the input is 1-->2-->3-->4-->5, then the output should be 1-->5-->2-->4-->3.

In other words, if the original linked list is first -> second -> third -> ……….->thirdlast -> secondlast -> last, then the modified linked list would be first -> last -> second -> second_last -> ...


1. All the node values are positive.

2. The size of the linked list is greater than 1.

3. The end of the linked list is represented by -1.
Input Format:
The first line of the input contains an integer T denoting the number of test cases.

The first and the only line of each test case contains the values of nodes of the linked list L, which is to be modified as shown above.

Output format :

For each test case, print a single line containing space-separated integers denoting the values of the modified linked list.

The output of each test case will be printed in a separate line.
You do not have to print anything, it has already been taken care of. Just implement the given function. Also, update the given linked list in place.


1 <= T <= 50
1 <= N <= 10 ^ 4

Where 'N' is the number of nodes in a linked list.

Time Limit: 1 sec.