Given a linked list and two keys in it, swap nodes for two given keys. Nodes should be swapped by changing links. Swapping data of nodes may be expensive in many situations when data contains many fields.

It may be assumed that all keys in linked list are distinct.

Examples:

Input:  10->15->12->13->20->14,  x = 12, y = 20
Output: 10->15->20->13->12->14

Input:  10->15->12->13->20->14,  x = 10, y = 20
Output: 20->15->12->13->10->14

Input:  10->15->12->13->20->14,  x = 12, y = 13
Output: 10->15->13->12->20->14

This may look a simple problem, but is interesting question as it has following cases to be handled.
1) x and y may or may not be adjacent.
2) Either x or y may be a head node.
3) Either x or y may be last node.
4) x and/or y may not be present in linked list.

[ad type=”banner”]

Let us write a clear working code that handles all the above cases:

The idea is to first search x and y in given linked list. If any of them is not present, then return. While searching for x and y, keep track of current and previous pointers. First change next of previous pointers, then change next of current pointers. Following is Java implementation of this approach.

Java Programming For Swap Nodes in a Linked List:

[pastacode lang=”java” manual=”%2F%2F%20Java%20program%20to%20swap%20two%20given%20nodes%20of%20a%20linked%20list%0A%20%0Aclass%20Node%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20Node%20next%3B%0A%20%20%20%20Node(int%20d)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20data%20%3D%20d%3B%0A%20%20%20%20%20%20%20%20next%20%3D%20null%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0Aclass%20LinkedList%0A%7B%0A%20%20%20%20Node%20head%3B%20%2F%2F%20head%20of%20list%0A%20%0A%20%20%20%20%2F*%20Function%20to%20swap%20Nodes%20x%20and%20y%20in%20linked%20list%20by%0A%20%20%20%20%20%20%20changing%20links%20*%2F%0A%20%20%20%20public%20void%20swapNodes(int%20x%2C%20int%20y)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Nothing%20to%20do%20if%20x%20and%20y%20are%20same%0A%20%20%20%20%20%20%20%20if%20(x%20%3D%3D%20y)%20return%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Search%20for%20x%20(keep%20track%20of%20prevX%20and%20CurrX)%0A%20%20%20%20%20%20%20%20Node%20prevX%20%3D%20null%2C%20currX%20%3D%20head%3B%0A%20%20%20%20%20%20%20%20while%20(currX%20!%3D%20null%20%26%26%20currX.data%20!%3D%20x)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20prevX%20%3D%20currX%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20currX%20%3D%20currX.next%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Search%20for%20y%20(keep%20track%20of%20prevY%20and%20currY)%0A%20%20%20%20%20%20%20%20Node%20prevY%20%3D%20null%2C%20currY%20%3D%20head%3B%0A%20%20%20%20%20%20%20%20while%20(currY%20!%3D%20null%20%26%26%20currY.data%20!%3D%20y)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20prevY%20%3D%20currY%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20currY%20%3D%20currY.next%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20either%20x%20or%20y%20is%20not%20present%2C%20nothing%20to%20do%0A%20%20%20%20%20%20%20%20if%20(currX%20%3D%3D%20null%20%7C%7C%20currY%20%3D%3D%20null)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20x%20is%20not%20head%20of%20linked%20list%0A%20%20%20%20%20%20%20%20if%20(prevX%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%20%20%20%20prevX.next%20%3D%20currY%3B%0A%20%20%20%20%20%20%20%20else%20%2F%2Fmake%20y%20the%20new%20head%0A%20%20%20%20%20%20%20%20%20%20%20%20head%20%3D%20currY%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20y%20is%20not%20head%20of%20linked%20list%0A%20%20%20%20%20%20%20%20if%20(prevY%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%20%20%20%20prevY.next%20%3D%20currX%3B%0A%20%20%20%20%20%20%20%20else%20%2F%2F%20make%20x%20the%20new%20head%0A%20%20%20%20%20%20%20%20%20%20%20%20head%20%3D%20currX%3B%0A%20%20%20%20%20%20%20%20Node%20temp%20%3D%20currX.next%3B%0A%20%20%20%20%20%20%20%20currX.next%20%3D%20currY.next%3B%0A%20%20%20%20%20%20%20%20currY.next%20%3D%20temp%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Function%20to%20add%20Node%20at%20beginning%20of%20list.%20*%2F%0A%20%20%20%20public%20void%20push(int%20new_data)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20Node%20new_Node%20%3D%20new%20Node(new_data)%3B%0A%20%0A%20%20%20%20%20%20%20%20new_Node.next%20%3D%20head%3B%0A%20%0A%20%0A%20%20%20%20%20%20%20%20head%20%3D%20new_Node%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20public%20void%20printList()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Node%20tNode%20%3D%20head%3B%0A%20%20%20%20%20%20%20%20while%20(tNode%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(tNode.data%2B%22%20%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20tNode%20%3D%20tNode.next%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20LinkedList%20llist%20%3D%20new%20LinkedList()%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20The%20constructed%20linked%20list%20is%3A%0A%20%20%20%20%20%20%20%20%20%20%20%201-%3E2-%3E3-%3E4-%3E5-%3E6-%3E7%20*%2F%0A%20%20%20%20%20%20%20%20llist.push(7)%3B%0A%20%20%20%20%20%20%20%20llist.push(6)%3B%0A%20%20%20%20%20%20%20%20llist.push(5)%3B%0A%20%20%20%20%20%20%20%20llist.push(4)%3B%0A%20%20%20%20%20%20%20%20llist.push(3)%3B%0A%20%20%20%20%20%20%20%20llist.push(2)%3B%0A%20%20%20%20%20%20%20%20llist.push(1)%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.print(%22%5Cn%20Linked%20list%20before%20calling%20swapNodes()%20%22)%3B%0A%20%20%20%20%20%20%20%20llist.printList()%3B%0A%20%0A%20%20%20%20%20%20%20%20llist.swapNodes(4%2C%203)%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.print(%22%5Cn%20Linked%20list%20after%20calling%20swapNodes()%20%22)%3B%0A%20%20%20%20%20%20%20%20llist.printList()%3B%0A%20%20%20%20%7D%0A%7D%0A%2F%2F%20This%20code%20is%20contributed%20by%20Rajat%20Mishra” message=”” highlight=”” provider=”manual”/]

Output:

 Linked list before calling swapNodes() 1 2 3 4 5 6 7
 Linked list after calling swapNodes() 1 2 4 3 5 6 7

Optimizations:

The code can be optimized to search x and y in single traversal. Two loops are used to keep program simple.

[ad type=”banner”]