# How to swap nodes in a linked list without Swapping data?

Given a linked list and two keys in it, swap the node for two given keys. You need to change the link to replace the node. If the data contains many fields, exchanging data at the node can be expensive in many situations.

It can assume that all keys in linked list are separate.

This may seem like a simple matter, but it is an interesting question because you need to handle the following cases:

1. x and y may not or may be adjacent.
2. X or y can be the main node.
3. X or y can be the last node.
4. x and/or and may not be present in the linked list.

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

It is highly recommended to minimize your browser and try it yourself first.

The idea of â€‹â€‹looking for x and y first in a given linked list. If any of them donâ€™t exist, they will come back. Keeps track of current and previous pointers while searching for x and y. First change the next to the previous pointer, then change the next to the current pointer.

#### Here Code:

```#include <bits/stdc++.h>
using namespace std;
class Node {
public:
Â Â Â Â Â Â Â Â  int data;
Â Â Â Â Â Â Â Â  Node* next;
};
void swapNodes(Node** head_ref, int x, int y)
{
Â Â Â Â Â Â Â Â  if (x == y)
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  return;
Â Â Â Â Â Â Â Â  Node *prevX = NULL, *currX = *head_ref;
Â Â Â Â Â Â Â Â  while (currX && currX->data != x) {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  prevX = currX;
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  currX = currX->next;
Â Â Â Â Â Â Â Â  }
Â Â Â Â Â Â Â Â  Node *prevY = NULL, *currY = *head_ref;
Â Â Â Â Â Â Â Â  while (currY && currY->data != y) {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  prevY = currY;
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  currY = currY->next;
Â Â Â Â Â Â Â Â  }
Â Â Â Â Â Â Â Â  if (currX == NULL || currY == NULL)
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  return;
Â Â Â Â Â Â Â Â  if (prevX != NULL)
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  prevX->next = currY;
Â Â Â Â Â Â Â Â  else
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  *head_ref = currY;
Â Â Â Â Â Â Â Â  if (prevY != NULL)
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  prevY->next = currX;
Â Â Â Â Â Â Â Â  else
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  *head_ref = currX;
Â Â Â Â Â Â Â Â  Node* temp = currY->next;
Â Â Â Â Â Â Â Â  currY->next = currX->next;
Â Â Â Â Â Â Â Â  currX->next = temp;
}
{
Â Â Â Â Â Â Â Â  Node* new_node = new Node();
Â Â Â Â Â Â Â Â  new_node->data = new_data;
Â Â Â Â Â Â Â Â  new_node->next = (*head_ref);
Â Â Â Â Â Â Â Â  (*head_ref) = new_node;
}
void printList(Node* node)
{
Â Â Â Â Â Â Â Â  while (node != NULL) {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  cout << node->data << " ";
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  node = node->next;
Â Â Â Â Â Â Â Â  }
}
int main()
{
Â Â Â Â Â Â Â Â  Node* start = NULL;
Â Â Â Â Â Â Â Â  push(&start, 7);
Â Â Â Â Â Â Â Â  push(&start, 6);
Â Â Â Â Â Â Â Â  push(&start, 5);
Â Â Â Â Â Â Â Â  push(&start, 4);
Â Â Â Â Â Â Â Â  push(&start, 3);
Â Â Â Â Â Â Â Â  push(&start, 2);
Â Â Â Â Â Â Â Â  push(&start, 1);
Â Â Â Â Â Â Â Â  cout << "Linked list before calling swapNodes() ";
Â Â Â Â Â Â Â Â  printList(start);
Â Â Â Â Â Â Â Â  swapNodes(&start, 4, 3);
Â Â Â Â Â Â Â Â  cout << "\nLinked list after calling swapNodes() ";
Â Â Â Â Â Â Â Â  printList(start);
Â Â Â Â Â Â Â Â  return 0;
}```

### Optimization:

The above code can be optimized to find x and y in a single scan. Two loops are used to simplify the program.

### Simpler approach:

#### Here Code:

```#include <iostream>
using namespace std;
class Node {
public:
Â Â Â Â Â Â Â Â  int data;
Â Â Â Â Â Â Â Â  class Node* next;
Â Â Â Â Â Â Â Â  Node(int val, Node* next)
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  : data(val)
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  , next(next)
Â Â Â Â Â Â Â Â  {
Â Â Â Â Â Â Â Â  }
Â Â Â Â Â Â Â Â  void printList()
Â Â Â Â Â Â Â Â  {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  Node* node = this;
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  while (node != NULL) {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  cout << node->data << " ";
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  node = node->next;
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  }
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  cout << endl;
Â Â Â Â Â Â Â Â  }
};
{
Â Â Â Â Â Â Â Â  (*head_ref) = new Node(new_data, *head_ref);
}
void swap(Node*& a, Node*& b)
{
Â Â Â Â Â Â Â Â  Node* temp = a;
Â Â Â Â Â Â Â Â  a = b;
Â Â Â Â Â Â Â Â  b = temp;
}
void swapNodes(Node** head_ref, int x, int y)
{
Â Â Â Â Â Â Â Â  if (x == y)
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  return;
Â Â Â Â Â Â Â Â  Node **a = NULL, **b = NULL;
Â Â Â Â Â Â Â Â  while (*head_ref) {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  if ((*head_ref)->data == x) {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  a = head_ref;
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  }
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  else if ((*head_ref)->data == y) {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  b = head_ref;
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  }
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  head_ref = &((*head_ref)->next);
Â Â Â Â Â Â Â Â  }
Â Â Â Â Â Â Â Â  if (a && b) {
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  swap(*a, *b);
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  swap(((*a)->next), ((*b)->next));
Â Â Â Â Â Â Â Â  }
}
int main()
{
Â Â Â Â Â Â Â Â  Node* start = NULL;
Â Â Â Â Â Â Â Â  push(&start, 7);
Â Â Â Â Â Â Â Â  push(&start, 6);
Â Â Â Â Â Â Â Â  push(&start, 5);
Â Â Â Â Â Â Â Â  push(&start, 4);
Â Â Â Â Â Â Â Â  push(&start, 3);
Â Â Â Â Â Â Â Â  push(&start, 2);
Â Â Â Â Â Â Â Â  push(&start, 1);
Â Â Â Â Â Â Â Â  cout << "Linked list before calling swapNodes() ";
Â Â Â Â Â Â Â Â  start->printList();
Â Â Â Â Â Â Â Â  swapNodes(&start, 6, 1);
Â Â Â Â Â Â Â Â  cout << "Linked list after calling swapNodes() ";
Â Â Â Â Â Â Â Â  start->printList();
}```

### References:

(Visited 135 times, 1 visits today)