Move even nodes to the end of the list in reverse order

Rearrange the given linked list such that every even node will be moved to end of the list in reverse order.

 

For example,


Input:  1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> null

Output: 1 -> 3 -> 5 -> 7 -> 6 -> 4 -> 2 -> null

 

The idea is to use MoveNode() function. The function takes a node from the front of the source, and move it to the front of the destination. Here the source node will be even nodes in given list and destination will be a new list. After we have moved every even node, we simply append the new list which now contains the even nodes in reverse order to the original list.

C

Download   Run Code

Output:

Before : 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> null
After  : 1 -> 3 -> 5 -> 7 -> 6 -> 4 -> 2 -> null

Java

Download   Run Code

Output:

Before : 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> null
After  : 1 -> 3 -> 5 -> 7 -> 6 -> 4 -> 2 -> null

 
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

Loading...

Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂


Leave a Reply

avatar
  Subscribe  
Notify of