forked from anmol098/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swapNodesWithoutSwappingData.cpp
106 lines (89 loc) · 1.92 KB
/
swapNodesWithoutSwappingData.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Problem : Given a linked list, and two data values, x and y.
* Goal: Swap the nodes of linkedlist by swapping pointers
* (and not swapping data) such that nodes with data x will contain y and vice versa.
*
*/
#include <iostream>
struct Node {
int data;
Node* next;
Node(int d) : data{d}, next{nullptr} { }
};
/**** Algorithm Function ****/
void swapSpecial(Node* & head, int x, int y) {
//empty list
if (head == nullptr) {
return;
}
//nothing to do if both are values are same.
if ( x == y ) {
return;
}
Node * prevx = nullptr;
Node * currx = head;
Node * prevy = nullptr;
Node * curry = head;
//search for x
while ( currx && currx->data != x ) {
prevx = currx;
currx = currx->next;
}
//search for y
while ( curry && curry->data != y ) {
prevy = curry;
curry = curry->next;
}
//x or y not found
if ( currx == nullptr || curry == nullptr ) {
return;
}
//if x is not head
if ( prevx != nullptr ) {
prevx->next = curry;
} else {
head = curry;
}
//similarly if y is not head
if ( prevy != nullptr) {
prevy->next = currx;
} else {
head = currx;
}
//now lets swap the next pointers
Node *temp = curry->next;
curry->next = currx->next;
currx->next = temp;
}
void insert(Node* & head, int data) {
if (head == nullptr) {
head = new Node(data);
return;
}
Node *temp = head;
while(temp->next != nullptr) {
temp = temp->next;
}
temp->next = new Node(data);
}
void printList(Node * head) {
while (head) {
std::cout << head->data << "-->";
head = head->next;
}
std::cout << "null" << std::endl;
}
int main() {
Node *head = nullptr;
insert(head, 10);
insert(head, 15);
insert(head, 12);
insert(head, 13);
insert(head, 20);
insert(head, 14);
printList(head);
std::cout << "Replacing 12 with 20 and 20 with 12:\n";
swapSpecial(head, 12, 20);
printList(head);
return 0;
}