-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#145.cpp
59 lines (50 loc) · 992 Bytes
/
#145.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
#include<iostream>
using namespace std;
struct Node {
int val;
Node* next;
Node(int v) {
val = v;
next = nullptr;
}
};
void print(Node* head) {
while (head != nullptr) {
cout << head->val << " ";
head = head->next;
}
cout << endl;
}
Node* swap_every_two(Node* head) {
Node* prev = nullptr;
Node* a = head;
Node* b = head->next;
Node* new_head = (b == nullptr ? a : b);
while (b != nullptr) {
// set the node before the current pair
if (prev != nullptr) prev->next = b;
// swap the current pair
Node* temp = b->next;
b->next = a;
a->next = temp;
// set previous node and the next node pair
prev = a;
a = temp;
b = (a == nullptr) ? nullptr : a->next;
}
return new_head;
}
int main() {
Node *n1 = new Node(1);
Node *n2 = new Node(2);
Node *n3 = new Node(3);
Node *n4 = new Node(4);
Node *n5 = new Node(5);
n1->next = n2;
n2->next = n3;
n3->next = n4;
n4->next = n5;
print(n1);
Node* new_list = swap_every_two(n1);
print(new_list);
}