-
Notifications
You must be signed in to change notification settings - Fork 0
/
24-SwapNodesInPairs.cpp
59 lines (55 loc) · 1.71 KB
/
24-SwapNodesInPairs.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
/*=============================================================================
# FileName: 24-SwapNodesInPairs.cpp
# Desc: AC, 4ms
# Author: Jian Huang
# Email: [email protected]
# HomePage: https://cn.linkedin.com/in/huangjian1993
# Version: 0.0.1
# LastChange: 2015-08-03 19:41:31
# History:
=============================================================================*/
#include <leetcode.h>
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if (!head || !head->next) {
return head;
}
ListNode *prev = NULL, *first = head, *second = head->next, *nHead = second;
while (true) {
first->next = second->next;
second->next = first;
if (first != head) {
prev->next = second;
}
prev = first;
if (first->next && first->next->next) {
second = first->next->next;
first = first->next;
} else {
return nHead;
}
}
return nHead;
}
};
int main() {
ListNode *node1 = new ListNode(1), *node2 = new ListNode(2), *node3 = new ListNode(3), *node4 = new ListNode(4), *node5 = new ListNode(5);
node1->next = node2;
node2->next = node3;
node3->next = node4;
node4->next = node5;
Solution solution;
ListNode *head = solution.swapPairs(node1);
while (head) {
cout << head->val << " ";
head = head->next;
}
cout << endl;
return 0;
}