void swapNodeValues(int node1, int node2) {
Node* temp = head;
int N = 0;
while(temp != NULL) {
N++;
temp = temp->next;
}
if(node1 < 1 || node1 > N || node2 < 1 || node2 > N)
return;
Node* pos1 = head;
Node* pos2 = head;
for(int i = 1; i < node1; i++) {
pos1 = pos1->next;
}
for(int i = 1; i < node2; i++) {
pos2 = pos2->next;
}
int val = pos1->data;
pos1->data = pos2->data;
pos2->data = val;
}
#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;
}
void push(Node** head_ref, int new_data)
{
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 << "
Linked list after calling swapNodes() ";
printList(start);
return 0;
}
public class Solution {
class Node {
int data;
Node next;
public Node(int data)
{
this.data = data;
this.next = null;
}
}
public Node head = null;
public Node tail = null;
public void addNode(int data)
{
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
}
else {
tail.next = newNode;
tail = newNode;
}
}
public void swap(int n1, int n2)
{
Node prevNode1 = null, prevNode2 = null,
node1 = head, node2 = head;
if (head == null) {
return;
}
if (n1 == n2)
return;
while (node1 != null && node1.data != n1) {
prevNode1 = node1;
node1 = node1.next;
}
while (node2 != null && node2.data != n2) {
prevNode2 = node2;
node2 = node2.next;
}
if (node1 != null && node2 != null) {
if (prevNode1 != null)
prevNode1.next = node2;
else
head = node2;
if (prevNode2 != null)
prevNode2.next = node1;
else
head = node1;
Node temp = node1.next;
node1.next = node2.next;
node2.next = temp;
}
else {
System.out.println("Swapping is not possible");
}
}
public void display()
{
Node current = head;
if (head == null) {
System.out.println("List is empty");
return;
}
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public static void main(String[] args)
{
Solution sList = new Solution();
sList.addNode(1);
sList.addNode(2);
sList.addNode(3);
sList.addNode(4);
sList.addNode(5);
sList.addNode(6);
sList.addNode(7);
System.out.println("Original list: ");
sList.display();
sList.swap(6, 1);
System.out.println("List after swapping nodes: ");
sList.display();
}
}
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* prev;
Node* next;
Node(int data)
{
this->data = data;
this->prev = NULL;
this->next = NULL;
}
};
void print(Node* head)
{
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
void push(Node*& head, Node*& tail,
int item)
{
if (tail == NULL) {
Node* temp = new Node(item);
tail = temp;
head = temp;
}
else {
Node* temp = new Node(item);
tail->next = temp;
temp->prev = tail;
tail = temp;
}
}
pair<Node*, Node*> find(Node*& head,
int x, int y)
{
Node* N1 = NULL;
Node* N2 = NULL;
Node* temp = head;
while (temp != NULL) {
if (temp->data == x)
N1 = temp;
else if (temp->data == y)
N2 = temp;
temp = temp->next;
}
return make_pair(N1, N2);
}
void swap(Node*& head, Node*& tail,
int x, int y)
{
if (head == NULL || head->next == NULL
|| x == y)
return;
pair<Node*, Node*> p = find(head, x, y);
Node* Node1 = p.first;
Node* Node2 = p.second;
if (Node1 == head)
head = Node2;
else if (Node2 == head)
head = Node1;
if (Node1 == tail)
tail = Node2;
else if (Node2 == tail)
tail = Node1;
Node* temp;
temp = Node1->next;
Node1->next = Node2->next;
Node2->next = temp;
if (Node1->next != NULL)
Node1->next->prev = Node1;
if (Node2->next != NULL)
Node2->next->prev = Node2;
temp = Node1->prev;
Node1->prev = Node2->prev;
Node2->prev = temp;
if (Node1->prev != NULL)
Node1->prev->next = Node1;
if (Node2->prev != NULL)
Node2->prev->next = Node2;
}
int main()
{
Node* head = NULL;
Node* tail = NULL;
push(head, tail, 1);
push(head, tail, 8);
push(head, tail, 7);
push(head, tail, 9);
push(head, tail, 4);
int X = 1, Y = 4;
cout << "Before Swapping: ";
print(head);
swap(head, tail, X, Y);
cout << "After Swapping: ";
print(head);
return 0;
}