-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0024-Swap_Nodes_in_Pairs.cpp
176 lines (161 loc) · 3.97 KB
/
0024-Swap_Nodes_in_Pairs.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*******************************************************************************
* 0024-Swap_Nodes_in_Pairs.cpp
* Billy.Ljm
* 16 May 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/swap-nodes-in-pairs/
*
* Given a linked list, swap every two adjacent nodes and return its head. You
* must solve the problem without modifying the values in the list's nodes
* (i.e., only nodes themselves may be changed.)
*
* ===========
* My Approach
* ===========
* We just have to crawl through the list, remember the two nodes preceeding
* those to be swapped, and the child of the later node to be swapped so nothing
* is orphaned. Then we just carefully swap them.
*
* This has a time complexity of O(n) and space complexity of O(1), where n is
* the length of the linked list.
******************************************************************************/
#include <iostream>
#include <vector>
/**
* Definition for singly - linked list
*/
struct ListNode {
int val;
ListNode* next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode* next) : val(x), next(next) {}
};
/**
* Solution
*/
class Solution {
public:
/**
* Swaps every pair in a linked list (by address, not value)
*
* @param head head of linked list to swap
*
* @return head of linked list with every pair swapped
*/
ListNode* swapPairs(ListNode* head) {
// if 0 or 1 element, no pairs to swap
if (head == nullptr or head->next == nullptr) {
return head;
}
// else swap all pairs
else {
ListNode* node1 = head;
ListNode* node2 = head->next;
ListNode* tmp;
// swap 0th and 1st node
tmp = node2->next;
node2->next = node1;
node1->next = tmp;
head = node2; // node2 - node1 - c - d
// move to next pair
for (int i = 0; i < 2; i++) { // a - node1 - node2 - d
if (node2->next == nullptr) break;
node2 = node2->next;
}
// swap every subsequent pair
while (node2->next != nullptr) {
// swap nodes
tmp = node2->next->next;
node1->next = node2->next;
node1->next->next = node2;
node2->next = tmp; // node1 - b - node2 - d
// move to next pair
for (int i = 0; i < 1; i++) { // node1 - b - c - node2
if (node2->next == nullptr) break;
node2 = node2->next;
}
for (int i = 0; i < 2; i++) { // a - b - node1 - node2
if (node1->next == nullptr) break;
node1 = node1->next;
}
}
return head;
}
}
};
/**
* Convert from vector to linked list
*
* @param nodes value of linked list nodes in order
*
* @return head of linked list
*/
ListNode* linkedlist(std::vector<int> nodes) {
ListNode* head = nullptr;
for (int i = nodes.size() - 1; i >= 0; i--) {
head = new ListNode(nodes[i], head);
}
return head;
}
/**
* Delete linked list
*
* @param head head of linked list to be deleted
*/
void dellinkedlist(ListNode* head) {
if (head == nullptr) {
; // nothing to delete
}
else if (head->next == nullptr) {
delete head;
}
else {
dellinkedlist(head->next);
delete head;
}
}
/**
* << operator for ListNodes's linked lisst
*/
std::ostream& operator<<(std::ostream& os, const ListNode* head) {
if (head == nullptr) {
return os;
}
else if (head->next == nullptr) {
return os << head->val;
}
else {
return os << head->val << "," << head->next;
}
}
/**
* Test cases
*/
int main(void) {
Solution sol;
ListNode* head;
std::vector<int> nodes;
// test case 1
nodes = { 1, 2, 3, 4 };
head = linkedlist(nodes);
std::cout << "swapPairs([" << head << "]) = ";
std::cout << "[" << sol.swapPairs(head) << "]" << std::endl;
dellinkedlist(head);
// test case 2
nodes = { };
head = linkedlist(nodes);
std::cout << "swapPairs([" << head << "]) = ";
std::cout << "[" << sol.swapPairs(head) << "]" << std::endl;
dellinkedlist(head);
// test case 3
nodes = { 1 };
head = linkedlist(nodes);
std::cout << "swapPairs([" << head << "]) = ";
std::cout << "[" << sol.swapPairs(head) << "]" << std::endl;
dellinkedlist(head);
return 0;
}