目录
一、题目描述
给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 。
进阶:
- 你可以在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序吗?
示例 1:
输入:head = [4,2,1,3]
输出:[1,2,3,4]
示例 2:
输入:head = [-1,5,3,4,0]
输出:[-1,0,3,4,5]
示例 3:
输入:head = []
输出:[]
提示:
- 链表中节点的数目在范围 [0, 5 * 104] 内
- -105 <= Node.val <= 105
二、解题思路
从常规的排序算法入手,快排的平均时间复杂度为logn级别,但是最坏可以达到二次方级别,空间要求为常数;归并排序时间复杂度满足要求,但常规的空间复杂度为logn级别。综上,常规的这两种都不是最符合题目要求的,但是提交都是可以通过的。
要完全符合要求的话,需要对归并排序进行修改,不能使用递归的归并,需要改为迭代。方式为将链表划分为一段一段有序的短链表,将短链表逐步合并为有序的长链表,当链表长度为1时,是有序的。所以短链的长度依次区1,2,4,8…逐步合并,最终有序。
三、代码实现
下面用c++把上述方法依次实现一遍
#include<bits/stdc++.h>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
ListNode* partition(ListNode* low, ListNode* high) {
//每次选最左边的结点的值作为基准值
int value = low->val;
//slow结点作为小于基准值的坑位
ListNode* slow = low;
//quick结点作为遍历结点
ListNode* quick = low->next;
while (quick != high) {
if (quick->val < value) {
slow = slow->next;
swap(slow->val, quick->val);
}
quick = quick->next;
}
//把基准值放在正确的位置
swap(low->val, slow->val);
return slow;
}
//方法一:链表快排
void quickSortList(ListNode* low, ListNode* high) {
if (low == high) return;
ListNode* parNode = partition(low, high);
quickSortList(low, parNode);
quickSortList(parNode->next, high);
}
//方法二:链表归并排序
ListNode* mergeList(ListNode* head1, ListNode* head2) {
ListNode* newHead = new ListNode(0);
ListNode* res = newHead;
ListNode* tmp1 = head1;
ListNode* tmp2 = head2;
while (tmp1 != nullptr && tmp2 != nullptr) {
if (tmp1->val < tmp2->val) {
newHead->next = tmp1;
tmp1 = tmp1->next;
} else {
newHead->next = tmp2;
tmp2 = tmp2->next;
}
newHead = newHead->next;
}
if (tmp1) {
newHead->next = tmp1;
}
if (tmp2) {
newHead->next = tmp2;
}
return res->next;
}
ListNode* mergeSort(ListNode* head, ListNode* tail) {
if (head == nullptr || head->next == nullptr) return head;
ListNode* slow = head;
ListNode* quick = head;
//必须加quick->next->next != tail,防止2->4->null这种情况会无限分裂下去
while (quick != nullptr && quick->next != nullptr && quick->next->next != tail) {
slow = slow->next;
quick = quick->next->next;
}
ListNode* mid = slow->next;
slow->next = nullptr;
return mergeList(mergeSort(head, slow->next), mergeSort(mid, tail));
}
ListNode* sortList(ListNode* head) {
// quickSortList(head, nullptr);
// return head;
return mergeSort(head, nullptr);
}
//题解所述方法:自低向上归并
//subLength = 1: (3->4)->(1->7)->(8->9)->(2->11)->(5->6)
//subLength = 2 : (1->3->4->7)->(2->8->9->11)->(5->6)
//subLength = 4 : (1->2->3->4->7->8->9->11)->(5->6)
//subLength = 8 : (1->2->3->4->5->6->7->8->9->11)
ListNode* sortList1(ListNode* head) {
if (head == nullptr) {
return head;
}
int length = 0;
ListNode* node = head;
while (node != nullptr) {
length++;
node = node->next;
}
ListNode* dummyHead = new ListNode(0, head);
for (int subLength = 1; subLength < length; subLength <<= 1) {
ListNode* prev = dummyHead, *curr = dummyHead->next;
//将链表进行分段,逐段合并
while (curr != nullptr) {
ListNode* head1 = curr;
for (int i = 1; i < subLength && curr->next != nullptr; i++) {
curr = curr->next;
}
ListNode* head2 = curr->next;
curr->next = nullptr;
curr = head2;
for (int i = 1; i < subLength && curr != nullptr && curr->next != nullptr; i++) {
curr = curr->next;
}
ListNode* next = nullptr;
if (curr != nullptr) {
next = curr->next;
curr->next = nullptr;
}
ListNode* merged = mergeList(head1, head2);
prev->next = merged;
while (prev->next != nullptr) {
prev = prev->next;
}
curr = next;
}
}
return dummyHead->next;
}
int main() {
ListNode* head = new ListNode(4);
head->next = new ListNode(2);
head->next->next = new ListNode(1);
head->next->next->next = new ListNode(3);
// head->next->next->next->next = new ListNode(0);
head = sortList(head);
ListNode* tmp = head;
while (tmp != nullptr) {
cout << tmp->val << " ";
tmp = tmp->next;
}
return 0;
}