-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
70 lines (59 loc) · 1.55 KB
/
main.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
#include <iostream>
using namespace std;
/*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:
// Runtime: 4 ms, faster than 97.17%
// Memory Usage: 8.2 MB, less than 76.71%
// 就是新旧两个链表头,然后不断从旧链表头部摘取出来一个,然后插入到新链表的头部
ListNode* reverseList(ListNode* head) {
ListNode* newHead = NULL;
while (head) {
ListNode* tmp = head->next;
head->next = newHead;
newHead = head;
head = tmp;
}
return newHead;
}
};
ostream &operator<<(ostream &os, ListNode* rtn) {
os << "[";
while (rtn) {
cout << rtn->val << ", ";
rtn = rtn->next;
}
os << "]";
return os;
}
ListNode * generator(int N) {
ListNode* rst = NULL;
ListNode* pre = NULL;
for (int i =0; N >=0 && i < N; i++) {
ListNode* tmp = new ListNode(i+1);
if (!rst) {
rst = tmp;
pre = tmp;
} else {
pre->next = tmp;
pre = tmp;
}
}
return rst;
}
int main(int argc, const char * argv[]) {
Solution gua;
ListNode* rtn;
ListNode* e1 = generator(5);
cout << "Before odd even swap:\n" << e1 << endl;
rtn = gua.reverseList(e1);
cout << "after odd even swap:\n" << rtn << endl;
return 0;
}