(LeetCodeHot100)148. 排序链表——sort-list

148. 排序链表——sort-list

给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表

示例 1:

img

1
2
输入:head = [4,2,1,3]
输出:[1,2,3,4]

示例 2:

img

1
2
输入:head = [-1,5,3,4,0]
输出:[-1,0,3,4,5]

示例 3:

1
2
输入:head = []
输出:[]

提示:

  • 链表中节点的数目在范围 [0, 5 * 104]
  • -105 <= Node.val <= 105

进阶:你可以在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序吗?


我的答案15ms|击败23.45%

  • 这样写肯定是不够的,时间空间复杂度极高
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
class Solution {
public ListNode sortList(ListNode head) {
List<Integer> list = new ArrayList<>();
ListNode p = head;
while (p != null) {
list.add(p.val);
p = p.next;
}
Collections.sort(list);
ListNode ans = new ListNode();
ans.next = null;
Iterator<Integer> iterator = list.iterator();
if (iterator.hasNext()) {
ListNode newNode = new ListNode();
ans.next = newNode;
newNode.next = null;
newNode.val = iterator.next();
} else {
return ans.next;
}
if (iterator.hasNext()) {
ListNode p2 = ans.next;
while (iterator.hasNext()) {
ListNode newNode = new ListNode();
newNode.next = null;
p2.next = newNode;
newNode.val = iterator.next();
p2 = p2.next;
}
}
return ans.next;
}
}

官方答案

前言

147. 对链表进行插入排序」要求使用插入排序的方法对链表进行排序,插入排序的时间复杂度是 O(n2),其中 n 是链表的长度。这道题考虑时间复杂度更低的排序算法。题目的进阶问题要求达到 O(nlogn) 的时间复杂度和 O(1) 的空间复杂度,时间复杂度是 O(nlogn) 的排序算法包括归并排序、堆排序和快速排序(快速排序的最差时间复杂度是 O(n2)),其中最适合链表的排序算法是归并排序。

归并排序基于分治算法。最容易想到的实现方式是自顶向下的递归实现,考虑到递归调用的栈空间,自顶向下归并排序的空间复杂度是 O(logn)。如果要达到 O(1) 的空间复杂度,则需要使用自底向上的实现方式。

方法一:自顶向下归并排序

  • 这个比方法二好理解,但是这个空间复杂度高(用了新的空间)

对链表自顶向下归并排序的过程如下。

  1. 找到链表的中点,以中点为分界,将链表拆分成两个子链表。寻找链表的中点可以使用快慢指针的做法,快指针每次移动 2 步,慢指针每次移动 1 步,当快指针到达链表末尾时,慢指针指向的链表节点即为链表的中点。
  2. 对两个子链表分别排序。
  3. 将两个排序后的子链表合并,得到完整的排序后的链表。可以使用「21. 合并两个有序链表」的做法,将两个有序的子链表进行合并。

上述过程可以通过递归实现。递归的终止条件是链表的节点个数小于或等于 1,即当链表为空或者链表只包含 1 个节点时,不需要对链表进行拆分和排序。

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
class Solution {
public ListNode sortList(ListNode head) {
return sortList(head, null);
}

public ListNode sortList(ListNode head, ListNode tail) {
if (head == null) {
return head;
}
if (head.next == tail) {
head.next = null;
return head;
}
ListNode slow = head, fast = head;
while (fast != tail) {
slow = slow.next;
fast = fast.next;
if (fast != tail) {
fast = fast.next;
}
}
ListNode mid = slow;
ListNode list1 = sortList(head, mid);
ListNode list2 = sortList(mid, tail);
ListNode sorted = merge(list1, list2);
return sorted;
}

public ListNode merge(ListNode head1, ListNode head2) {
ListNode dummyHead = new ListNode(0);
ListNode temp = dummyHead, temp1 = head1, temp2 = head2;
while (temp1 != null && temp2 != null) {
if (temp1.val <= temp2.val) {
temp.next = temp1;
temp1 = temp1.next;
} else {
temp.next = temp2;
temp2 = temp2.next;
}
temp = temp.next;
}
if (temp1 != null) {
temp.next = temp1;
} else if (temp2 != null) {
temp.next = temp2;
}
return dummyHead.next;
}
}

复杂度分析

  • 时间复杂度:O(nlogn),其中 n 是链表的长度。
  • 空间复杂度:O(logn),其中 n 是链表的长度。空间复杂度主要取决于递归调用的栈空间。

方法二:自底向上归并排序

  • 这个代码还真挺妙的,从小开始分,分了再合并,直到合并完

使用自底向上的方法实现归并排序,则可以达到 O(1) 的空间复杂度。

首先求得链表的长度 length,然后将链表拆分成子链表进行合并。

具体做法如下。

  1. subLength 表示每次需要排序的子链表的长度,初始时 subLength=1。
  2. 每次将链表拆分成若干个长度为 subLength 的子链表(最后一个子链表的长度可以小于 subLength),按照每两个子链表一组进行合并,合并后即可得到若干个长度为 subLength×2 的有序子链表(最后一个子链表的长度可以小于 subLength×2)。合并两个子链表仍然使用「21. 合并两个有序链表」的做法。
  3. subLength 的值加倍,重复第 2 步,对更长的有序子链表进行合并操作,直到有序子链表的长度大于或等于 length,整个链表排序完毕。

如何保证每次合并之后得到的子链表都是有序的呢?可以通过数学归纳法证明。

  1. 初始时 subLength=1,每个长度为 1 的子链表都是有序的。
  2. 如果每个长度为 subLength 的子链表已经有序,合并两个长度为 subLength 的有序子链表,得到长度为 subLength×2 的子链表,一定也是有序的。
  3. 当最后一个子链表的长度小于 subLength 时,该子链表也是有序的,合并两个有序子链表之后得到的子链表一定也是有序的。

因此可以保证最后得到的链表是有序的。

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
class Solution {
public ListNode sortList(ListNode head) {
if (head == null) {
return head;
}
int length = 0;
ListNode node = head;
while (node != null) {
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 != null) {
ListNode head1 = curr;
for (int i = 1; i < subLength && curr.next != null; i++) {
curr = curr.next;
}
ListNode head2 = curr.next;
curr.next = null;
curr = head2;
for (int i = 1; i < subLength && curr != null && curr.next != null; i++) {
curr = curr.next;
}
ListNode next = null;
if (curr != null) {
next = curr.next;
curr.next = null;
}
ListNode merged = merge(head1, head2);
prev.next = merged;
while (prev.next != null) {
prev = prev.next;
}
curr = next;
}
}
return dummyHead.next;
}

public ListNode merge(ListNode head1, ListNode head2) {
ListNode dummyHead = new ListNode(0);
ListNode temp = dummyHead, temp1 = head1, temp2 = head2;
while (temp1 != null && temp2 != null) {
if (temp1.val <= temp2.val) {
temp.next = temp1;
temp1 = temp1.next;
} else {
temp.next = temp2;
temp2 = temp2.next;
}
temp = temp.next;
}
if (temp1 != null) {
temp.next = temp1;
} else if (temp2 != null) {
temp.next = temp2;
}
return dummyHead.next;
}
}

复杂度分析

  • 时间复杂度:O(nlogn),其中 n 是链表的长度。
  • 空间复杂度:O(1)。

关键逻辑

  • 哨兵节点(dummyHead):val=0,next 指向原链表头(2)。
  • 作用:简化链表拼接逻辑(不用单独处理 “新链表头节点”,避免断链)。
  • 例子中初始状态:dummyHead(0) → 2 → 1 → 3 → 4
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
// subLength:当前要拆分的子链表长度(从1开始,每次翻倍)
for (int subLength = 1; subLength < length; subLength <<= 1) {
ListNode prev = dummyHead; // 用于拼接合并后的子链表(每次从哨兵节点开始)
ListNode curr = dummyHead.next; // 用于遍历原链表,拆分出子链表(初始指向2)

while (curr != null) { // 只要还有未拆分的节点,就继续
// --------------- 1. 拆分第一个子链表(head1)---------------
ListNode head1 = curr; // 第一个子链表的头(初始是2)
// 移动curr,让head1的长度=subLength(循环subLength-1次,因为已经占了curr的初始位置)
for (int i = 1; i < subLength && curr.next != null; i++) {
curr = curr.next; // 例子中subLength=1时,循环不执行,curr仍指向2
}

// --------------- 2. 拆分第二个子链表(head2)---------------
ListNode head2 = curr.next; // 第二个子链表的头(例子中subLength=1时,head2=1)
curr.next = null; // 切断head1和后面的联系(此时head1是[2],独立子链表)
curr = head2; // curr移到head2,准备拆分第二个子链表

// 移动curr,让head2的长度=subLength(如果剩余节点不足,就取到末尾)
for (int i = 1; i < subLength && curr != null && curr.next != null; i++) {
curr = curr.next; // 例子中subLength=1时,循环不执行,curr仍指向1
}

// --------------- 3. 保存下一轮拆分的起始位置(next)---------------
ListNode next = null;
if (curr != null) {
next = curr.next; // 下一轮要拆分的起始节点(例子中subLength=1时,next=3)
curr.next = null; // 切断head2和后面的联系(此时head2是[1],独立子链表)
}

// --------------- 4. 合并两个有序子链表(head1和head2)---------------
ListNode merged = merge(head1, head2); // 合并[2]和[1]→[1→2]
prev.next = merged; // 把合并后的子链表拼到prev后面(此时dummyHead→1→2)

// --------------- 5. 移动prev到合并链表的尾部(准备下一次拼接)---------------
while (prev.next != null) {
prev = prev.next; // 例子中prev从dummyHead移到2(合并链表的尾部)
}

// --------------- 6. curr移到下一轮拆分的起始位置,重复循环 ---------------
curr = next; // 例子中curr=3,开始下一次拆分
}
}