-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy path24-swap-nodes-in-pairs.cpp
More file actions
172 lines (143 loc) · 4.29 KB
/
24-swap-nodes-in-pairs.cpp
File metadata and controls
172 lines (143 loc) · 4.29 KB
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
/*
Problem 24: Swap Nodes in Pairs
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).
Example 1:
Input: head = [1,2,3,4]
Output: [2,1,4,3]
Example 2:
Input: head = []
Output: []
Example 3:
Input: head = [1]
Output: [1]
Constraints:
- The number of nodes in the list is in the range [0, 100].
- 0 <= Node.val <= 100
*/
#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) {}
};
class Solution {
public:
// Approach 1: Iterative
ListNode* swapPairs(ListNode* head) {
if (!head || !head->next) {
return head;
}
ListNode dummy(0);
dummy.next = head;
ListNode* prev = &dummy;
while (prev->next && prev->next->next) {
ListNode* first = prev->next;
ListNode* second = prev->next->next;
// Swapping
first->next = second->next;
second->next = first;
prev->next = second;
// Move to the next pair
prev = first;
}
return dummy.next;
}
// Approach 2: Recursive
ListNode* swapPairsRecursive(ListNode* head) {
// Base case: no node or only one node, nothing to swap
if (!head || !head->next) {
return head;
}
// Nodes to be swapped
ListNode* first = head;
ListNode* second = head->next;
// Recurse for the rest of the list
first->next = swapPairsRecursive(second->next);
second->next = first;
// New head of the swapped pair is 'second'
return second;
}
};
// Helper function to print the list
void printList(ListNode* head) {
ListNode* current = head;
std::cout << "[";
while (current) {
std::cout << current->val;
if (current->next) {
std::cout << ",";
}
current = current->next;
}
std::cout << "]\n";
}
// Helper function to create a list from a vector
ListNode* createList(const std::vector<int>& arr) {
if (arr.empty()) {
return nullptr;
}
ListNode* head = new ListNode(arr[0]);
ListNode* current = head;
for (size_t i = 1; i < arr.size(); ++i) {
current->next = new ListNode(arr[i]);
current = current->next;
}
return head;
}
// Helper function to delete a list
void deleteList(ListNode* head) {
ListNode* current = head;
while (current) {
ListNode* next_node = current->next;
delete current;
current = next_node;
}
}
int main() {
Solution sol;
std::cout << "Testing Swap Nodes in Pairs (Iterative):\n";
// Test 1
ListNode* head1 = createList({1, 2, 3, 4});
std::cout << "Input: [1,2,3,4] -> Output: ";
ListNode* result1 = sol.swapPairs(head1);
printList(result1); // Expected: [2,1,4,3]
deleteList(result1);
// Test 2
ListNode* head2 = createList({});
std::cout << "Input: [] -> Output: ";
ListNode* result2 = sol.swapPairs(head2);
printList(result2); // Expected: []
deleteList(result2);
// Test 3
ListNode* head3 = createList({1});
std::cout << "Input: [1] -> Output: ";
ListNode* result3 = sol.swapPairs(head3);
printList(result3); // Expected: [1]
deleteList(result3);
// Test 4
ListNode* head4 = createList({1, 2, 3});
std::cout << "Input: [1,2,3] -> Output: ";
ListNode* result4 = sol.swapPairs(head4);
printList(result4); // Expected: [2,1,3]
deleteList(result4);
std::cout << "\nTesting Swap Nodes in Pairs (Recursive):\n";
// Test 5
ListNode* head5 = createList({1, 2, 3, 4});
std::cout << "Input: [1,2,3,4] -> Output: ";
ListNode* result5 = sol.swapPairsRecursive(head5);
printList(result5); // Expected: [2,1,4,3]
deleteList(result5);
// Test 6
ListNode* head6 = createList({});
std::cout << "Input: [] -> Output: ";
ListNode* result6 = sol.swapPairsRecursive(head6);
printList(result6); // Expected: []
deleteList(result6);
return 0;
}