Skip to content

Commit 4f0ea3f

Browse files
author
changhongyuan
committed
21题
1 parent d63d318 commit 4f0ea3f

File tree

3 files changed

+151
-1
lines changed

3 files changed

+151
-1
lines changed

.idea/leetcode/editor.xml

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/com/changhongyuan/leetcode/editor/cn/[21]合并两个有序链表.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,28 @@ class MergeTwoSortedLists{
5252
*/
5353
class Solution {
5454
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
55-
return null;
55+
if (null == list1) {
56+
return list2;
57+
}
58+
if (null == list2) {
59+
return list1;
60+
}
61+
if (list1.val < list2.val) {
62+
list1.next = mergeTwoLists(list1.next, list2);
63+
return list1;
64+
} else {
65+
list2.next = mergeTwoLists(list2.next, list1);
66+
return list2;
67+
}
5668
}
5769
}
5870
//leetcode submit region end(Prohibit modification and deletion)
5971

6072
public static void main(String[] args) {
6173
Solution solution = new MergeTwoSortedLists().new Solution();
74+
ListNode list1 = new ListNode(1, new ListNode(2, new ListNode(4)));
75+
ListNode list2 = new ListNode(1, new ListNode(3, new ListNode(4)));
76+
ListNode result = solution.mergeTwoLists(list1, list2);
77+
System.out.println(result.toString());
6278
}
6379
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
### 思路
2+
3+
- 标签:链表、递归
4+
- 这道题可以使用递归实现,新链表也不需要构造新节点,我们下面列举递归三个要素
5+
- 终止条件:两条链表分别名为 `l1` 和 `l2`,当 `l1` 为空或 `l2` 为空时结束
6+
- 返回值:每一层调用都返回排序好的链表头
7+
- 本级递归内容:如果 `l1` 的 `val` 值更小,则将 `l1.next` 与排序好的链表头相接,`l2` 同理
8+
- $O(m+n)$,$m$ 为 `l1`的长度,$n$ 为 `l2` 的长度
9+
10+
### 代码
11+
12+
* []
13+
14+
```Java
15+
/**
16+
* Definition for singly-linked list.
17+
* public class ListNode {
18+
* int val;
19+
* ListNode next;
20+
* ListNode(int x) { val = x; }
21+
* }
22+
*/
23+
class Solution {
24+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
25+
if(l1 == null) {
26+
return l2;
27+
}
28+
if(l2 == null) {
29+
return l1;
30+
}
31+
32+
if(l1.val < l2.val) {
33+
l1.next = mergeTwoLists(l1.next, l2);
34+
return l1;
35+
} else {
36+
l2.next = mergeTwoLists(l1, l2.next);
37+
return l2;
38+
}
39+
}
40+
}
41+
```
42+
43+
* []
44+
45+
```JavaScript
46+
/**
47+
* Definition for singly-linked list.
48+
* function ListNode(val) {
49+
* this.val = val;
50+
* this.next = null;
51+
* }
52+
*/
53+
/**
54+
* @param {ListNode} l1
55+
* @param {ListNode} l2
56+
* @return {ListNode}
57+
*/
58+
var mergeTwoLists = function(l1, l2) {
59+
if(l1 === null){
60+
return l2;
61+
}
62+
if(l2 === null){
63+
return l1;
64+
}
65+
if(l1.val < l2.val){
66+
l1.next = mergeTwoLists(l1.next, l2);
67+
return l1;
68+
}else{
69+
l2.next = mergeTwoLists(l1, l2.next);
70+
return l2;
71+
}
72+
};
73+
```
74+
75+
### 画解
76+
77+
<![frame_00001.png](https://pic.leetcode-cn.com/7ddaf1beb64fdef4393cc6ebd0dfd1723b97d2c183ab5c8414c0898027623a00-frame_00001.png),![frame_00002.png](https://pic.leetcode-cn.com/f4b7e354473d2bf28283ac3c410bc81e9f7ecb35f14189de9fadc041452c2653-frame_00002.png),![frame_00003.png](https://pic.leetcode-cn.com/001e4c2fdd8b5d725bc25df6373f7590404d9ef16efdea6e3700b68c23500a7a-frame_00003.png),![frame_00004.png](https://pic.leetcode-cn.com/5fbc72d56f32a8b1bc34db4bbd1588abebb4942348d8ea22fdb60724c8e4986c-frame_00004.png),![frame_00007.png](https://pic.leetcode-cn.com/912a9fef02ca9d5b4cdb891c2500f496a5f329adafa22f8ecdfd1cb591434b92-frame_00007.png)>
78+
79+
想看大鹏画解更多高频面试题,欢迎阅读大鹏的 LeetBook:[《画解剑指 Offer 》](https://leetcode-cn.com/leetbook/detail/illustrate-lcof/),O(∩_∩)O

0 commit comments

Comments
 (0)