Move front node of the given list to the front of the another list

Given a linked list, move front node of it to the front of the another given list.

 

For example,

Input:

First List – 1 -> 2 -> 3 -> null
Second List – 6 -> 4 -> 2 -> null

Output:

First List – 6 -> 1 -> 2 -> 3 -> null
Second List – 4 -> 2 -> null

 


 

This is a variant on Push(). Instead of creating a new node and pushing it onto the given list, it takes two lists, removes the front node from the second list and pushes it onto the front of the first. This turns out to be a handy utility function to have for several later problems.

 
C++ implementation –
 

Download   Run Code

Output:

First List – 6 -> 1 -> 2 -> 3 -> null
Second List – 4 -> 2 -> null

 
Source:
http://cslibrary.stanford.edu/105/LinkedListProblems.pdf

 
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 🙂