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”]

How to write a clean working code that handles all of the above possibilities.

The idea it 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 python implementation of this approach.

Python Programming:

[pastacode lang=”python” manual=”%23%20Python%20program%20to%20swap%20two%20given%20nodes%20of%20a%20linked%20list%0Aclass%20LinkedList(object)%3A%0A%20%20%20%20def%20__init__(self)%3A%0A%20%20%20%20%20%20%20%20self.head%20%3D%20None%0A%20%0A%20%20%20%20%23%20head%20of%20list%0A%20%20%20%20class%20Node(object)%3A%0A%20%20%20%20%20%20%20%20def%20__init__(self%2C%20d)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self.data%20%3D%20d%0A%20%20%20%20%20%20%20%20%20%20%20%20self.next%20%3D%20None%0A%20%0A%20%20%20%20%23%20Function%20to%20swap%20Nodes%20x%20and%20y%20in%20linked%20list%20by%0A%20%20%20%20%23%20changing%20links%0A%20%20%20%20def%20swapNodes(self%2C%20x%2C%20y)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%20Nothing%20to%20do%20if%20x%20and%20y%20are%20same%0A%20%20%20%20%20%20%20%20if%20x%20%3D%3D%20y%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%0A%20%0A%20%20%20%20%20%20%20%20%23%20Search%20for%20x%20(keep%20track%20of%20prevX%20and%20CurrX)%0A%20%20%20%20%20%20%20%20prevX%20%3D%20None%0A%20%20%20%20%20%20%20%20currX%20%3D%20self.head%0A%20%20%20%20%20%20%20%20while%20currX%20!%3D%20None%20and%20currX.data%20!%3D%20x%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20prevX%20%3D%20currX%0A%20%20%20%20%20%20%20%20%20%20%20%20currX%20%3D%20currX.next%0A%20%0A%20%20%20%20%20%20%20%20%23%20Search%20for%20y%20(keep%20track%20of%20prevY%20and%20currY)%0A%20%20%20%20%20%20%20%20prevY%20%3D%20None%0A%20%20%20%20%20%20%20%20currY%20%3D%20self.head%0A%20%20%20%20%20%20%20%20while%20currY%20!%3D%20None%20and%20currY.data%20!%3D%20y%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20prevY%20%3D%20currY%0A%20%20%20%20%20%20%20%20%20%20%20%20currY%20%3D%20currY.next%0A%20%0A%20%20%20%20%20%20%20%20%23%20If%20either%20x%20or%20y%20is%20not%20present%2C%20nothing%20to%20do%0A%20%20%20%20%20%20%20%20if%20currX%20%3D%3D%20None%20or%20currY%20%3D%3D%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%0A%20%20%20%20%20%20%20%20%23%20If%20x%20is%20not%20head%20of%20linked%20list%0A%20%20%20%20%20%20%20%20if%20prevX%20!%3D%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20prevX.next%20%3D%20currY%0A%20%20%20%20%20%20%20%20else%3A%20%23make%20y%20the%20new%20head%0A%20%20%20%20%20%20%20%20%20%20%20%20self.head%20%3D%20currY%0A%20%0A%20%20%20%20%20%20%20%20%23%20If%20y%20is%20not%20head%20of%20linked%20list%0A%20%20%20%20%20%20%20%20if%20prevY%20!%3D%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20prevY.next%20%3D%20currX%0A%20%20%20%20%20%20%20%20else%3A%20%23%20make%20x%20the%20new%20head%0A%20%20%20%20%20%20%20%20%20%20%20%20self.head%20%3D%20currX%0A%20%0A%20%20%20%20%20%20%20%20%23%20Swap%20next%20pointers%0A%20%20%20%20%20%20%20%20temp%20%3D%20currX.next%0A%20%20%20%20%20%20%20%20currX.next%20%3D%20currY.next%0A%20%20%20%20%20%20%20%20currY.next%20%3D%20temp%0A%20%0A%20%20%20%20%23%20Function%20to%20add%20Node%20at%20beginning%20of%20list.%0A%20%20%20%20def%20push(self%2C%20new_data)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%201.%20alloc%20the%20Node%20and%20put%20the%20data%0A%20%20%20%20%20%20%20%20new_Node%20%3D%20self.Node(new_data)%0A%20%0A%20%20%20%20%20%20%20%20%23%202.%20Make%20next%20of%20new%20Node%20as%20head%0A%20%20%20%20%20%20%20%20new_Node.next%20%3D%20self.head%0A%20%0A%20%20%20%20%20%20%20%20%23%203.%20Move%20the%20head%20to%20point%20to%20new%20Node%0A%20%20%20%20%20%20%20%20self.head%20%3D%20new_Node%0A%20%0A%20%20%20%20%23%20This%20function%20prints%20contents%20of%20linked%20list%20starting%0A%20%20%20%20%23%20from%20the%20given%20Node%0A%20%20%20%20def%20printList(self)%3A%0A%20%20%20%20%20%20%20%20tNode%20%3D%20self.head%0A%20%20%20%20%20%20%20%20while%20tNode%20!%3D%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print%20tNode.data%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20tNode%20%3D%20tNode.next%0A%20%0A%23%20Driver%20program%20to%20test%20above%20function%0Allist%20%3D%20LinkedList()%0A%20%0A%23%20The%20constructed%20linked%20list%20is%3A%0A%23%201-%3E2-%3E3-%3E4-%3E5-%3E6-%3E7%0Allist.push(7)%0Allist.push(6)%0Allist.push(5)%0Allist.push(4)%0Allist.push(3)%0Allist.push(2)%0Allist.push(1)%0Aprint%20%22Linked%20list%20before%20calling%20swapNodes()%20%22%0Allist.printList()%0Allist.swapNodes(4%2C%203)%0Aprint%20%22%5CnLinked%20list%20after%20calling%20swapNodes()%20%22%0Allist.printList()%0A%20%0A%23%20This%20code%20is%20contributed%20by%20BHAVYA%20JAIN” 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 above code can be optimized to search x and y in single traversal. Two loops are used to keep program simple.

[ad type=”banner”]