-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreorder-list.cpp
45 lines (41 loc) · 1.02 KB
/
reorder-list.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
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
void reorderList(ListNode *head) {
ListNode * cur, * past, *next;
cur = head;
int cnt = 0;
while(cur != NULL) {
cnt++;
if(cur->next != NULL) {
past = cur;
cur = cur->next;
}
else
break;
}
if(cnt < 3)
return ;
next = head->next;
head->next = cur;
past->next = NULL;
cur->next = next;
}
};
int main() {
Solution a;
ListNode * bla = new ListNode(1);
bla->next = new ListNode(2);
bla->next->next = new ListNode(3);
bla->next->next->next = NULL;
a.reorderList(bla);
for(ListNode * head = bla; head != NULL; head = head->next) {
cout << head->val << endl;
}
}