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:

// Java program to swap two given nodes of a linked list

class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}

class LinkedList
{
Node head; // head of list

/* Function to swap Nodes x and y in linked list by
changing links */
public void swapNodes(int x, int y)
{
// Nothing to do if x and y are same
if (x == y) return;

// Search for x (keep track of prevX and CurrX)
Node prevX = null, currX = head;
while (currX != null && currX.data != x)
{
prevX = currX;
currX = currX.next;
}

// Search for y (keep track of prevY and currY)
Node prevY = null, currY = head;
while (currY != null && currY.data != y)
{
prevY = currY;
currY = currY.next;
}

// If either x or y is not present, nothing to do
if (currX == null || currY == null)
return;

// If x is not head of linked list
if (prevX != null)
prevX.next = currY;
else //make y the new head
head = currY;

// If y is not head of linked list
if (prevY != null)
prevY.next = currX;
else // make x the new head
head = currX;
Node temp = currX.next;
currX.next = currY.next;
currY.next = temp;
}

/* Function to add Node at beginning of list. */
public void push(int new_data)
{

Node new_Node = new Node(new_data);

new_Node.next = head;


head = new_Node;
}

public void printList()
{
Node tNode = head;
while (tNode != null)
{
System.out.print(tNode.data+" ");
tNode = tNode.next;
}
}


public static void main(String[] args)
{
LinkedList llist = new LinkedList();

/* The constructed linked list is:
1->2->3->4->5->6->7 */
llist.push(7);
llist.push(6);
llist.push(5);
llist.push(4);
llist.push(3);
llist.push(2);
llist.push(1);

System.out.print("\n Linked list before calling swapNodes() ");
llist.printList();

llist.swapNodes(4, 3);

System.out.print("\n Linked list after calling swapNodes() ");
llist.printList();
}
}
// This code is contributed by Rajat Mishra

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