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 from previous post. 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++ implementation –
 

Download   Run Code

Output:

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

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz