-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathL0148_SortList.java
72 lines (63 loc) · 1.86 KB
/
L0148_SortList.java
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
import common.ListNode;
/**
* https://leetcode.cn/problems/sort-list/
*
* 给你链表的头结点 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 * 10⁴] 内
* - -10⁵ <= Node.val <= 10⁵
*/
public class L0148_SortList {
public 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 mid = slow.next;
slow.next = null;
// 递归排序两个子链表
ListNode left = sortList(head);
ListNode right = sortList(mid);
// 合并两个有序链表
return merge(left, right);
}
// 合并两个有序链表
private ListNode merge(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode curr = dummy;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
curr.next = l1;
l1 = l1.next;
} else {
curr.next = l2;
l2 = l2.next;
}
curr = curr.next;
}
curr.next = l1 != null ? l1 : l2;
return dummy.next;
}
}