-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24.cpp
More file actions
51 lines (41 loc) · 1.43 KB
/
24.cpp
File metadata and controls
51 lines (41 loc) · 1.43 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
// Problem : 24. Swap Nodes in Pairs
// Link : https://leetcode.com/problems/swap-nodes-in-pairs/
#include <iostream>
using namespace std;
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:
ListNode *swapPairs(ListNode *head) {
if (head == nullptr || head->next == nullptr) // Edge case handle, edge cases : [], [1]
return head;
ListNode *newHead = new ListNode(-1, head); // new virtual head
ListNode *curr = newHead; // current node pointer
while (curr->next->next != nullptr) {
ListNode *temp = curr->next; // save the next node
curr->next = temp->next; // connect current node to node 2 ahead
curr = curr->next; //move 1 step
temp->next = curr->next;
curr->next = temp; // insert temp node 2 pos ahead of it's original
curr = curr->next;
if (curr->next == nullptr) //edge case
break;
}
return newHead->next;
}
};
int main() {
ListNode head = ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, nullptr))));
Solution ob;
ListNode* output = ob.swapPairs(&head);
while (output != nullptr){
cout << output->val << endl;
output = output->next;
}
return 0;
}