Skip to content

Commit 697d9c4

Browse files
committed
四刷21
1 parent 98b564d commit 697d9c4

File tree

3 files changed

+76
-9
lines changed

3 files changed

+76
-9
lines changed

Diff for: docs/0021-merge-two-sorted-lists.adoc

+38-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
11
[#0021-merge-two-sorted-lists]
2-
= 21. Merge Two Sorted Lists
2+
= 21. 合并两个有序链表
33

4-
{leetcode}/problems/merge-two-sorted-lists/[LeetCode - Merge Two Sorted Lists^]
4+
https://leetcode.cn/problems/merge-two-sorted-lists/[LeetCode - 21. 合并两个有序链表 ^]
55

6-
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
6+
将两个升序链表合并为一个新的 *升序* 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
7+
8+
*示例 1:*
79

810
image::images/0021-00.jpg[{image_attr}]
911

10-
*Example:*
11-
[subs="verbatim,quotes,macros"]
12-
----
13-
*Input:* 1->2->4, 1->3->4
14-
*Output:* 1->1->2->3->4->4
15-
----
12+
....
13+
输入:l1 = [1,2,4], l2 = [1,3,4]
14+
输出:[1,1,2,3,4,4]
15+
....
16+
17+
*示例 2:*
18+
19+
....
20+
输入:l1 = [], l2 = []
21+
输出:[]
22+
....
23+
24+
*示例 3:*
25+
26+
....
27+
输入:l1 = [], l2 = [0]
28+
输出:[0]
29+
....
30+
31+
*提示:*
32+
33+
* 两个链表的节点数目范围是 `[0, 50]`
34+
* `+-100 <= Node.val <= 100+`
35+
* `l1``l2` 均按 *非递减顺序* 排列
1636
1737
== 思路分析
1838

@@ -69,6 +89,15 @@ include::{sourcedir}/_0021_MergeTwoSortedLists_2.java[tag=answer]
6989
include::{sourcedir}/_0021_MergeTwoSortedLists_3.java[tag=answer]
7090
----
7191
--
92+
93+
四刷::
94+
+
95+
--
96+
[{java_src_attr}]
97+
----
98+
include::{sourcedir}/_0021_MergeTwoSortedLists_4.java[tag=answer]
99+
----
100+
--
72101
====
73102

74103

Diff for: logbook/202503.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ endif::[]
150150
|{doc_base_url}/0053-maximum-subarray.adoc[题解]
151151
|⭕️ 动态规划。稀里糊涂就对了,还要对推演。另有更精妙的分治解法,抽空再尝试。
152152

153+
|{counter:codes2503}
154+
|{leetcode_base_url}/merge-two-sorted-lists/[21. Merge Two Sorted Lists^]
155+
|{doc_base_url}/0021-merge-two-sorted-lists.adoc[题解]
156+
|✅
157+
153158
|===
154159

155160
截止目前,本轮练习一共完成 {codes2503} 道题。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.ListNode;
4+
5+
public class _0021_MergeTwoSortedLists_4 {
6+
// tag::answer[]
7+
/**
8+
* @author D瓜哥 · https://www.diguage.com
9+
* @since 2025-04-05 23:39:31
10+
*/
11+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
12+
ListNode dummy = new ListNode(0);
13+
ListNode cur = dummy;
14+
while (l1 != null && l2 != null) {
15+
if (l1.val <= l2.val) {
16+
cur.next = l1;
17+
l1 = l1.next;
18+
} else {
19+
cur.next = l2;
20+
l2 = l2.next;
21+
}
22+
cur = cur.next;
23+
}
24+
if (l1 != null) {
25+
cur.next = l1;
26+
}
27+
if (l2 != null) {
28+
cur.next = l2;
29+
}
30+
return dummy.next;
31+
}
32+
// end::answer[]
33+
}

0 commit comments

Comments
 (0)