Given a linked list, swap the k'th node from the beginning with the k'th node from the end. The swapping should be done so that only links between the nodes are exchanged, and no data is swapped.

For example,

Input:
 
Linked List: 1 —> 2 —> 3 —> 4 —> 5 —> 6 —> 7 —> 8 —> NULL
k = 2
 
Output: 1 —> 7 —> 3 —> 4 —> 5 —> 6 —> 2 —> 8 —> NULL

Practice this problem

The idea is to traverse the linked list and find pointers to the k'th node from the beginning and the end. Then swap their pointers. This looks easy enough, but the code needs to handle several boundary cases while exchanging the links, such as both nodes being adjacent to each other, one node is a head node, or both nodes doesn’t exist (when k is more than the total number of nodes in a linked list).

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

C


Download  Run Code

Output:

Before: 1 —> 2 —> 3 —> 4 —> 5 —> 6 —> 7 —> 8 —> NULL
After : 1 —> 7 —> 3 —> 4 —> 5 —> 6 —> 2 —> 8 —> NULL

Java


Download  Run Code

Output:

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

Python


Download  Run Code

Output:

Before : 1 —> 2 —> 3 —> 4 —> 5 —> 6 —> 7 —> 8 —> None
After : 1 —> 7 —> 3 —> 4 —> 5 —> 6 —> 2 —> 8 —> None

The time complexity of the above solution is O(n), where n is the total number of nodes in the linked list, and doesn’t require any extra space.