Problem title
Difficulty
Avg time to solve

Reverse Alternate Nodes of a Singly Linked List
Moderate
--
Nearest Multiple of 10
Easy
15 mins
Leftmost & Rightmost Nodes of Binary Tree
Easy
20 mins
Minimum Spanning Tree
Moderate
34 mins
Top View
Moderate
42 mins
Replace 0s
Moderate
30 mins
Print Name
Easy
5 mins
Ninja And The LCM
Easy
15 mins
Minimum Swaps To Make Identical Array
Moderate
--
Unique Subsets
Moderate
45 mins
4

# Reverse Alternate Nodes of a Singly Linked List

Difficulty: MEDIUM
Contributed By

Problem Statement

#### For Example:

``````The given linked list is 1->2->3->4 then after reversing the alternate nodes we will have 1->3->4->2.

Assuming 0 based indexing, odd indexed nodes are the alternate nodes that is, in the given linked list the node with value 2 and the node with value 4 are the alternate nodes.

List without alternate nodes:  1->3
List with alternate nodes:  2->4
Reversing the list with alternate nodes: 4->2

After appending the reversed alternate nodes at the end, the updated list will be 1->3->4->2.
``````
##### Input format :
``````The first and the only line of input contains the elements of the singly linked list separated by a single space and terminated by -1 and hence -1 would never be a list element.
``````
##### Output format :
``````For each test case, print a single line containing space-separated integers denoting the elements of the resultant linked list and ending with -1 denoting the end of the linked list.

The output of each test case will be printed in a separate line.
``````

#### Note:

``````You do not need to print anything; it has already been taken care of. Just implement the given function.
``````
##### Constraints :
``````0 <= N <= 10 ^ 6
-10 ^ 9 <= DATA <= 10 ^ 9 and DATA != -1

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

Time Limit: 1 sec.
``````
##### Sample Input 1 :
``````2 4 6 8 10 -1
``````
##### Sample Output 1 :
``````2 6 10 8 4 -1
``````
##### Explanation to Sample Input 1 :
``````The given linked list is 2->4->6->8->10 then after reversing the alternate nodes we will have 2->6->10->8->4.

Assuming 0 based indexing, odd indexed nodes are the alternate nodes that is, in the given linked list the node with value 4 and the node with value 8 are the alternate nodes.

List without alternate nodes:  2->6->10
List with alternate nodes:  4->8
Reversing the list with alternate nodes: 8->4

After appending the reversed alternate nodes at the end, the updated list will be 2->6->10->8->4.
``````
##### Sample Input 2 :
``````-10 20 -1
``````
##### Sample Output 2 :
`````` -10 20 -1
``````
Console