-
Notifications
You must be signed in to change notification settings - Fork 2
/
148.Sort List.cpp
57 lines (49 loc) · 1.1 KB
/
148.Sort 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
class Solution
{
public:
ListNode *sortList(ListNode *head){
if (head==NULL||head->next==NULL){
return head;
}
ListNode *slow=head,*fast=head;
//假定head存放数据
while (fast->next!=NULL&&fast->next->next!=NULL){
fast=fast->next->next;
slow=slow->next;
}
ListNode *head1=slow->next;
ListNode *head2=head;
slow->next=NULL;//slow指向null
head1=sortList(head1);
head2=sortList(head2);
return merge(head1,head2);
}
ListNode *merge(ListNode *lh,ListNode *rh){
ListNode *temp=new ListNode(0);
ListNode *p=temp;
while (lh!=NULL&&rh!=NULL){
if (lh->val<rh->val){
p->next=lh;
lh=lh->next;
}
else{
p->next=rh;
rh=rh->next;
}
p=p->next;
//这个时候第一轮循环下来 p->rh ,需要使得p指向下一个
}
//检测有没有剩余
if (lh!=NULL){
p->next=lh;
}else{
p->next=rh;
}
//p现在到了后面,当然也有可能最后面 没想到一个链表也能递归啊
p=temp->next;
temp->next=NULL;//让temp指向null
delete temp;
return p;
}
// 快慢指针的方法非常好啊,至少快慢指针掌握了
};