-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_list.cpp
More file actions
70 lines (66 loc) · 1.83 KB
/
sort_list.cpp
File metadata and controls
70 lines (66 loc) · 1.83 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
65
66
67
68
69
/**
* 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 first node 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
if (head == NULL || head->next == NULL) {
return head;
}
ListNode* preMid = getMidPre(head);
ListNode* right = sortList(preMid->next);
preMid->next = NULL;
ListNode* left = sortList(head);
return mergeSortedList(left, right);
}
ListNode *mergeSortedList(ListNode *a, ListNode *b) {
ListNode dummy(0);
ListNode *p = &dummy;
while (a != NULL && b != NULL) {
if (a->val <= b->val) {
p->next = a;
a = a->next;
} else {
p->next = b;
b = b->next;
}
p = p->next;
}
if (a != NULL) {
p->next = a;
}
if (b != NULL) {
p->next = b;
}
return dummy.next;
}
ListNode *getMidPre(ListNode *head) {
if (head == NULL) {
return head;
}
ListNode* slow = head;
ListNode* fast = head->next;
//主要是为了handle 2个node的情况,返回mid前一个node的原因是,
//可以通过->next得到后半段的链表,同时通过->next = null截断前半部分的链表
while (fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
};