-
Notifications
You must be signed in to change notification settings - Fork 0
/
recorder_list.cpp
59 lines (56 loc) · 1.04 KB
/
recorder_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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#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) {
if(head == NULL)
return;
ListNode *p = head;
ListNode *tp = head;
while(tp->next != NULL)
tp = tp->next;
return myorder(p, tp);
}
void myorder(ListNode *head, ListNode *tail){
if(head == tail)
return;
if(head->next == tail)
return;
ListNode *p;
p = head->next;
head->next = tail;
tail->next = p;
head = p;
while(p->next != tail)
p = p->next;
p->next = NULL;
tail = p;
return myorder(head, tail);
}
};
int main(){
Solution s;
ListNode *head = new ListNode(1);
ListNode *p;
p = head;
p->next = new ListNode(2);
p = p->next;
p->next = new ListNode(3);
p = p->next;
p->next = new ListNode(4);
s.reorderList(head);
while(head != NULL)
{
cout<<head->val<<endl;
head = head->next;
}
return 0;
}