
题目:leetcode 148
给你链表的头结点 head
,请将其按 升序 排列并返回 排序后的链表 。
进阶:
- 你可以在
O(n log n)
时间复杂度和常数级空间复杂度下,对链表进行排序吗?
知识点:
- 快慢双指针:慢的走一步,快的走两步,快的走到末尾时,慢的就为中间节点。(一般在链表应用中,特别是在归并思想中,要注意断开两张表的关联: slow->next=null)
ListNode *slow = head;
ListNode *fast = head->next;
while(fast != NULL && fast->next != NULL){
slow = slow->next;
fast = fast->next->next;
}
- 归并-递归解法,由于涉及到递归,必定不满足空间复杂度,但满足时间复杂度
//思路:归并排序,找中点和合并操作
ListNode* sortList(ListNode* head) {
if(head == NULL || head->next == NULL) return head;
ListNode *slow = head;
ListNode *fast = head->next;
while(fast != NULL && fast->next != NULL){
slow = slow->next;
fast = fast->next->next;
}
ListNode *middleNode = slow->next;
slow->next = NULL; // 断开
ListNode *left = sortList(head);
ListNode *right = sortList(middleNode);
return mergeSortList(left, right);
}
ListNode *mergeSortList(ListNode *left, ListNode *right){
ListNode *dummy = new ListNode(-1);
ListNode *cur = dummy;
while(left != NULL && right != NULL){
if(left->val < right->val){
cur->next = left;
left = left->next;
}else{
cur->next = right;
right = right->next;
}
cur = cur->next;
}
while(left != NULL){
cur->next = left;
cur = cur->next;
left = left->next;
}
while(right != NULL){
cur->next = right;
cur = cur->next;
right = right->next;
}
return dummy->next;
}
2. 归并,由下至上
bottom-to-up 的归并思路是这样的:先两个两个的 merge,完成一趟后,再 4 个4个的 merge,直到结束。举个简单的例子:[4,3,1,7,8,9,2,11,5,6]
.
step=1: (3->4)->(1->7)->(8->9)->(2->11)->(5->6)
step=2: (1->3->4->7)->(2->8->9->11)->(5->6)
step=4: (1->2->3->4->7->8->9->11)->(5->6)
step=8: (1->2->3->4->5->6->7->8->9->11)
链表里操作最难掌握的应该就是各种断链啊,然后再挂接啊。在这里,我们主要用到链表操作的两个技术:
merge(l1, l2)
,双路归并,我相信这个操作大家已经非常熟练的,就不做介绍了。cut(l, n)
,可能有些同学没有听说过,它其实就是一种 split 操作,即断链操作。不过我感觉使用 cut 更准确一些,它表示,将链表l
切掉前 n 个节点,并返回后半部分的链表头。- 额外再补充一个 dummyHead 大法,已经讲过无数次了,仔细体会吧。
---
希望同学们能把双路归并和 cut 断链的代码烂记于心,以后看到类似的题目能够刷到手软。
掌握了这三大神器后,我们的 bottom-to-up 算法伪代码就十分清晰了:
current = dummy.next;
tail = dummy;
for (step = 1; step < length; step *= 2) {
while (current) {
// left->@->@->@->@->@->@->null
left = current;
// left->@->@->null right->@->@->@->@->null
right = cut(current, step); // 将 current 切掉前 step 个头切下来。
// left->@->@->null right->@->@->null current->@->@->null
current = cut(right, step); // 将 right 切掉前 step 个头切下来。
// dummy.next -> @->@->@->@->null,最后一个节点是 tail,始终记录
// ^
// tail
tail.next = merge(left, right);
while (tail->next) tail = tail->next; // 保持 tail 为尾部
}
}
好了,下面是比较正式的代码。
class Solution {
public:
ListNode* sortList(ListNode* head) {
ListNode dummyHead(0);
dummyHead.next = head;
auto p = head;
int length = 0;
while (p) {
++length;
p = p->next;
}
for (int size = 1; size < length; size <<= 1) {//注意 <<=
auto cur = dummyHead.next;
auto tail = &dummyHead;
while (cur) {
auto left = cur;
auto right = cut(left, size); // left->@->@ right->@->@->@...
cur = cut(right, size); // left->@->@ right->@->@ cur->@->...
tail->next = merge(left, right);
while (tail->next) { // 注意 tail指向链表最后
tail = tail->next;
}
}
}
return dummyHead.next;
}
// 注意 cut n个节点的时候,实际上 cur =head已经处于第一个节点了,遍历一次就到第二个节点了
ListNode* cut(ListNode* head, int n) {
auto p = head;
while (--n && p) {
p = p->next;
}
if (!p) return nullptr;
auto next = p->next;
p->next = nullptr;
return next;
}
ListNode* merge(ListNode* l1, ListNode* l2) {
ListNode dummyHead(0);
auto p = &dummyHead;
while (l1 && l2) {
if (l1->val < l2->val) {
p->next = l1;
p = l1;
l1 = l1->next;
} else {
p->next = l2;
p = l2;
l2 = l2->next;
}
}
p->next = l1 ? l1 : l2;
return dummyHead.next;
}
};