-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path143.cpp
More file actions
64 lines (59 loc) · 1.12 KB
/
143.cpp
File metadata and controls
64 lines (59 loc) · 1.12 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
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
void reorderList(ListNode* head) {
ListNode *p=head,*a=head,*b=head;
ListNode *mid=findMidNode(head);
ListNode *q=reverseList(mid);
while(p!=NULL&&q!=NULL)
{
a=p;b=q;
p=p->next;q=q->next;
a->next=b;b->next=p;
}
if(q!=NULL)
b->next=q;
}
ListNode *findMidNode(ListNode *head)
{
ListNode *mid=head,*tail=head,*last=head;//last是mid之前的
while(tail!=NULL&&tail->next!=NULL)
{
last=mid;
mid=mid->next;
tail=tail->next->next;
}
if(last!=NULL)
last->next=NULL;
//把mid之前的last作为尾,断链
return mid;
}
ListNode *reverseList(ListNode *head)
{
if(head==NULL) return head;
ListNode *cur=head,*pre=NULL,*temp=NULL;
while(cur!=NULL)
{
temp=cur->next;
cur->next=pre;
pre=cur;
cur=temp;
}
return pre;
//cur为空时退出,最后一个元素是pre,
}
};