/
148.排序链表.cpp
116 lines (109 loc) · 2.67 KB
/
148.排序链表.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
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
/*
* @lc app=leetcode.cn id=148 lang=cpp
*
* [148] 排序链表
*
* https://leetcode-cn.com/problems/sort-list/description/
*
* algorithms
* Medium (63.96%)
* Likes: 428
* Dislikes: 0
* Total Accepted: 43.5K
* Total Submissions: 67.9K
* Testcase Example: '[4,2,1,3]'
*
* 在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。
*
* 示例 1:
*
* 输入: 4->2->1->3
* 输出: 1->2->3->4
*
*
* 示例 2:
*
* 输入: -1->5->3->4->0
* 输出: -1->0->3->4->5
*
*/
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
// @lc code=start
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* sortList(ListNode* head) {
// root = dummyHead
ListNode node = ListNode(-1);
auto root = &node;
root->next = head;
int len = 0;
while (head) {
++len;
head = head->next;
}
for (int sz = 1; sz < len; sz *= 2) {
// tail表示已经merge链表的尾部
auto cur = root->next, tail = root;
while (cur) {
auto left = cur;
auto right = cut(left, sz);
cur = cut(right, sz);
tail->next = merge(left, right);
while (tail->next) {
tail = tail->next;
}
}
}
return root->next;
}
// splite操作,切掉前n个节点,返回后半部分。
ListNode* cut(ListNode* head, int n) {
auto p = head;
while (--n && p)
p = p->next;
//防止head 没有n个节点
if(!p) return nullptr;
auto next = p->next;
p->next = nullptr;
return next;
}
// leetcode 21
ListNode* merge(ListNode* l1, ListNode* l2) {
// root == dummyHead
ListNode node = ListNode(0), *root = &node, *i = root;
while (l1 && l2) {
if (l1->val < l2->val) {
i->next = l1;
l1 = l1->next;
} else {
i->next = l2;
l2 = l2->next;
}
i = i->next;
}
i->next = l1 ? l1 : l2;
return root->next;
}
};
// @lc code=end
int main() {
Solution solution;
ListNode* root = new ListNode(-1);
root->next = new ListNode(5);
root->next->next = new ListNode(3);
root->next->next->next = new ListNode(4);
root->next->next->next->next = new ListNode(0);
ListNode* node = solution.sortList(root);
}