Skip to content

Commit 820a5ce

Browse files
committed
二刷148
1 parent aa473df commit 820a5ce

File tree

6 files changed

+115
-15
lines changed

6 files changed

+115
-15
lines changed

Diff for: docs/0148-sort-list.adoc

+48-15
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,65 @@
11
[#0148-sort-list]
2-
= 148. Sort List
2+
= 148. 排序链表
33

4-
{leetcode}/problems/sort-list/[LeetCode - Sort List^]
4+
https://leetcode.cn/problems/sort-list/[LeetCode - 148. 排序链表 ^]
55

6-
Sort a linked list in _O_(_n_ log _n_) time using constant space complexity.
6+
给你链表的头结点 `head` ,请将其按 *升序* 排列并返回 *排序后的链表*
77

8-
*Example 1:*
8+
*示例 1:*
99

10-
[subs="verbatim,quotes,macros"]
11-
----
12-
*Input:* 4->2->1->3
13-
*Output:* 1->2->3->4
10+
image::images/0148-01.jpg[{image_attr}]
1411

15-
----
12+
....
13+
输入:head = [4,2,1,3]
14+
输出:[1,2,3,4]
15+
....
1616

17-
*Example 2:*
17+
*示例 2:*
1818

19-
[subs="verbatim,quotes,macros"]
20-
----
21-
*Input:* -1->5->3->4->0
22-
*Output:* -1->0->3->4->5
23-
----
19+
image::images/0148-02.jpg[{image_attr}]
20+
21+
....
22+
输入:head = [-1,5,3,4,0]
23+
输出:[-1,0,3,4,5]
24+
....
25+
26+
*示例 3:*
27+
28+
....
29+
输入:head = []
30+
输出:[]
31+
....
2432

33+
*提示:*
2534

35+
* 链表中节点的数目在范围 `[0, 5 * 10^4^]`
36+
* `-10^5^ \<= Node.val \<= 10^5^`
37+
38+
**进阶:**你可以在 stem:[O(n*logn)] 时间复杂度和常数级空间复杂度下,对链表进行排序吗?
39+
40+
== 思路分析
2641

2742
[[src-0148]]
43+
[tabs]
44+
====
45+
一刷::
46+
+
47+
--
2848
[{java_src_attr}]
2949
----
3050
include::{sourcedir}/_0148_SortList.java[tag=answer]
3151
----
52+
--
53+
54+
二刷::
55+
+
56+
--
57+
[{java_src_attr}]
58+
----
59+
include::{sourcedir}/_0148_SortList_2.java[tag=answer]
60+
----
61+
--
62+
====
63+
64+
== 参考资料
3265

Diff for: docs/images/0148-01.jpg

20.6 KB
Loading

Diff for: docs/images/0148-02.jpg

25.8 KB
Loading

Diff for: logbook/202503.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ endif::[]
190190
|{doc_base_url}/0050-powx-n.adoc[题解]
191191
|✅ 递归很简单,抽空再思考一下非递归形式。
192192

193+
|{counter:codes2503}
194+
|{leetcode_base_url}/sort-list/[148. 排序链表]
195+
|{doc_base_url}/0148-sort-list.adoc[题解]
196+
|✅ 分治
197+
193198
|===
194199

195200
截止目前,本轮练习一共完成 {codes2503} 道题。

Diff for: src/main/java/com/diguage/algo/leetcode/_0148_SortList.java

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public class _0148_SortList {
3939
* Runtime: 4 ms, faster than 62.99% of Java online submissions for Sort List.
4040
*
4141
* Memory Usage: 40.2 MB, less than 89.47% of Java online submissions for Sort List.
42+
*
43+
* @author D瓜哥 · https://www.diguage.com
44+
* @since 2019-10-29 21:05
4245
*/
4346
public ListNode sortList(ListNode head) {
4447
if (Objects.isNull(head) || Objects.isNull(head.next)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.ListNode;
4+
import com.diguage.util.ListNodes;
5+
6+
import java.util.Arrays;
7+
8+
public class _0148_SortList_2 {
9+
// tag::answer[]
10+
/**
11+
* @author D瓜哥 · https://www.diguage.com
12+
* @since 2025-04-08 15:06:31
13+
*/
14+
public ListNode sortList(ListNode head) {
15+
if (head == null || head.next == null) {
16+
return head;
17+
}
18+
// 分
19+
ListNode pre = head, slow = head, fast = head;
20+
while (fast != null && fast.next != null) {
21+
pre = slow;
22+
slow = slow.next;
23+
fast = fast.next.next;
24+
}
25+
pre.next = null; // 将链表切成两段
26+
// 治
27+
ListNode r1 = sortList(head);
28+
ListNode r2 = sortList(slow);
29+
// 合
30+
return merge(r1, r2);
31+
}
32+
33+
private ListNode merge(ListNode l1, ListNode l2) {
34+
ListNode result = new ListNode(0);
35+
ListNode cur = result;
36+
while (l1 != null && l2 != null) {
37+
if (l1.val <= l2.val) {
38+
cur.next = l1;
39+
l1 = l1.next;
40+
} else {
41+
cur.next = l2;
42+
l2 = l2.next;
43+
}
44+
cur = cur.next;
45+
}
46+
if (l1 != null) {
47+
cur.next = l1;
48+
}
49+
if (l2 != null) {
50+
cur.next = l2;
51+
}
52+
return result.next;
53+
}
54+
// end::answer[]
55+
56+
public static void main(String[] args) {
57+
new _0148_SortList_2().sortList(ListNodes.build(Arrays.asList(4, 2, 1, 3)));
58+
}
59+
}

0 commit comments

Comments
 (0)