6

Modify Linked list

Difficulty: EASY
Contributed By
Avg. time to solve
10 min
Success Rate
90%

Problem Statement

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 -> ...

Note

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.
Note:
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.

Constraints:

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

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

Time Limit: 1 sec.
Sample Input 1:
2
1 2 3 4 -1
1 2 3 4 5 -1
Sample Output 1:
1 4 2 3
1 5 2 4 3

Explanation of Sample Input 1:

Test case 1:
The original Linked List  1 -> 2 -> 3 -> 4 is modified to 1 -> 4 -> 2 -> 3

Test case 2:
The original Linked List  1 -> 2 -> 3 -> 4 ->5 is modified to 1 -> 5 -> 2 ->4 ->3
Sample Input 2:
4
1 2 3 4 5 6 -1
1 -1
1 2 -1 
1 2 3 -1
Sample Output 2:
1 6 2 5 3 4 
1 
1 2 
1 3 2
Reset Code
Full screen
copy-code
Console