-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path98.h
134 lines (123 loc) · 4.02 KB
/
98.h
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* Definition of ListNode
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/*
* @param head: The head of linked list.
* @return: You should return the head of the sorted linked list, using constant space complexity.
*/
ListNode * sortList(ListNode * head) {
// write your code here
// 链表快排对于第一个要交换的数不好处理,单单只选择第一个可能退化为o(n2)
// 对于链表来说,归并排序容易些
//qsort(head, nullptr);
//return head;
return mergesort(head);
}
/*****************************qsort***********************************/
// qsort [head, end)
void qsort(ListNode *head, ListNode *end){
if(head == end) return;
if(head->next == end) return;
ListNode *p = __partition(head, end);
qsort(head, p);
qsort(p->next, end);
}
// qsort parition[head, end)
ListNode* __partition(ListNode *head, ListNode *end){
int v = head->val;
ListNode *p = head, *dummyhead = head;
dummyhead = dummyhead->next;
while(dummyhead != end){
if(dummyhead->val <= v){
p = p->next;
swap(p->val, dummyhead->val);
}
dummyhead = dummyhead->next;
}
swap(head->val, p->val);
return p;
}
/*****************************merge sort*****************************/
// merge sort[head, end)
ListNode* mergesort(ListNode *head){
if( head == nullptr || head->next == nullptr) return head;
ListNode *dummy = findmidNode(head);
ListNode *mid = dummy->next;
dummy->next = nullptr;
head = mergesort(head);
mid = mergesort(mid);
return merge(head, mid);
}
// find merge mid node[head, end)
ListNode *findmidNode(ListNode *head){
ListNode *fastnode = head, *slownode = head;
while(fastnode->next != nullptr && fastnode->next->next != nullptr){
slownode = slownode->next;
fastnode = fastnode->next->next;
}
return slownode;
}
// verson 1
// ListNode* merge(ListNode *head, ListNode *mid){
// ListNode auxhead(-1);
// auxhead.next = head;
// ListNode *dummyhead = &auxhead;
// ListNode *dummymid = mid;
// while(head != nullptr || dummymid != nullptr){
// if(head == nullptr){
// dummyhead->next = dummymid;
// dummyhead = dummymid;
// dummymid = dummymid->next;
// }
// else if( dummymid == nullptr){
// dummyhead->next = head;
// dummyhead = head;
// head = head->next;
// }
// else if(head->val < dummymid->val){
// dummyhead->next = head;
// dummyhead = head;
// head = head->next;
// }
// else{
// dummyhead->next = dummymid;
// dummyhead = dummymid;
// dummymid = dummymid->next;
// }
// }
// return auxhead.next;
// }
// verson 2
ListNode* merge(ListNode *head, ListNode *mid){
ListNode auxhead(-1);
auxhead.next = head;
ListNode *dummyhead = &auxhead;
ListNode *dummymid = mid;
while(head != nullptr && dummymid != nullptr){
if(head->val <= dummymid->val){
dummyhead->next = head;
dummyhead = head;
head = head->next;
}
else{
dummyhead->next = dummymid;
dummyhead = dummymid;
dummymid = dummymid->next;
}
}
if(head == nullptr) dummyhead->next = dummymid;
else dummyhead->next = head;
return auxhead.next;
}
};