-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort-list.cpp
69 lines (63 loc) · 1.99 KB
/
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
58
59
60
61
62
63
64
65
66
67
68
69
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
//总是忘记merge排序的过程,看了题解才能想起来:先排序后归并
//https://www.acwing.com/activity/content/problem/content/13/1/
//1. 先将母串从中间断开,分为长度相等的字串
//2. 然后分别将将两个字串进行排序
//3. 将两个字串merge为一个大串。
ListNode* sortList(ListNode* head) {
if(!head || !head->next) return head;
ListNode* s=head;
ListNode* f=head;
while(f->next && f->next->next){
s=s->next;
f=f->next->next;
}
f=s->next;
s->next=nullptr;
s=head;
s=sortList(s); // 注意用返回值给s重新赋值,这里一开始忘记了,还调试了一会儿,教训就是以后想清楚再开始敲代码。
f=sortList(f);
return merge(s,f);
}
ListNode* merge(ListNode* s, ListNode* f){
ListNode* head=new ListNode(INT_MIN);
ListNode* result=head;
while(s && f){
if(s->val<f->val){
head->next=s;
s=s->next;
}
else{
head->next=f;
f=f->next;
}
head=head->next;
}
if(s) head->next=s;
else head->next=f;
return result->next;
}
//先实现了一下插入排序,但是时间复杂度不符合题目要求。
ListNode* sortList1(ListNode* head) {
ListNode* newhead=new ListNode(INT_MIN);
ListNode* curr=newhead;
while(head){
ListNode* nhead=head->next;
while(curr->next && curr->next->val<head->val) curr=curr->next;
head->next=curr->next;
curr->next=head;
curr=newhead;
head=nhead;
}
return newhead->next;
}
};