Given two linked lists, move front node of the second list in front of the first 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

Practice this problem

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 moves it to the front of the first. This turns out to be a handy utility function to have for several later problems.

The algorithm can be implemented as follows in C, Java, and Python:

C


Download  Run Code

Output:

First List: 6 —> 1 —> 2 —> 3 —> NULL
Second List: 4 —> 2 —> NULL

Java


Download  Run Code

Output:

First List: 6 —> 1 —> 2 —> 3 —> null
Second List: 4 —> 2 —> null

Python


Download  Run Code

Output:

First List: 6 —> 1 —> 2 —> 3 —> None
Second List: 4 —> 2 —> None

The time complexity of the above solution is O(1).

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