Skip to content

Commit da5e9b2

Browse files
committed
2 parents fa802c5 + 4afdbd7 commit da5e9b2

File tree

5 files changed

+61
-3
lines changed

5 files changed

+61
-3
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Complete solutions to Leetcode problems, updated daily.
1313
| # | Title | Tags |
1414
|---|---|---|
1515
| 001 | [Two Sum](https://github.com/yanglbme/leetcode/tree/master/solution/001.Two%20Sum) | `Array`, `Hash Table` |
16+
| 021 | [Merge Two Sorted Lists](https://github.com/yanglbme/leetcode/tree/master/solution/021.Merge%20Two%20Sorted%20Lists) | `Linked List` |
1617
| 203 | [Remove Linked List Elements](https://github.com/yanglbme/leetcode/tree/master/solution/203.Remove%20Linked%20List%20Elements) | `Linked List` |
1718

1819
### Medium
@@ -33,7 +34,7 @@ I'm looking for long-term contributors/partners to this repo! Send me PRs if you
3334
## Contributors
3435

3536
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
36-
| <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="100px;"/><br /><sub><b>yanglbme</b></sub>](https://github.com/yanglbme)<br />[💻](https://github.com/yanglbme/leetcode/commits?author=yanglbme "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="100px;"/><br /><sub><b>yanglbme</b></sub>](https://github.com/yanglbme)<br />[💻](https://github.com/yanglbme/leetcode/commits?author=yanglbme "Code") </center> | <center>[<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="100px;"/><br /><sub><b>yanglbme</b></sub>](https://github.com/yanglbme)<br />[💻](https://github.com/yanglbme/leetcode/commits?author=yanglbme "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="100px;"/><br /><sub><b>yanglbme</b></sub>](https://github.com/yanglbme)<br />[💻](https://github.com/yanglbme/leetcode/commits?author=yanglbme "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="100px;"/><br /><sub><b>yanglbme</b></sub>](https://github.com/yanglbme)<br />[💻](https://github.com/yanglbme/leetcode/commits?author=yanglbme "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="100px;"/><br /><sub><b>yanglbme</b></sub>](https://github.com/yanglbme)<br />[💻](https://github.com/yanglbme/leetcode/commits?author=yanglbme "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="100px;"/><br /><sub><b>yanglbme</b></sub>](https://github.com/yanglbme)<br />[💻](https://github.com/yanglbme/leetcode/commits?author=yanglbme "Code") </center> |
37-
|---|---|---|---|---|---|---|
37+
| <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="100px;"/><br /><sub><b>yanglbme</b></sub>](https://github.com/yanglbme)<br />[💻](https://github.com/yanglbme/leetcode/commits?author=yanglbme "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/23625436?v=4" width="100px;"/><br /><sub><b>chakyam</b></sub>](https://github.com/chakyam)<br />[💻](https://github.com/yanglbme/leetcode/commits?author=chakyam "Code") </center> |
38+
|---|---|
3839

3940
<!-- ALL-CONTRIBUTORS-LIST:END -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## 合并两个有序链表
2+
### 题目描述
3+
4+
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
5+
6+
示例:
7+
```
8+
输入:1->2->4, 1->3->4
9+
输出:1->1->2->3->4->4
10+
```
11+
12+
### 解法
13+
利用链表天然的递归性。如果 l1 为空,返回 l2;如果 l2 为空,返回 l1。如果 `l1.val < l2.val`,返回 l1->mergeTwoLists(l1.next, l2);否则返回 l2->mergeTwoLists(l1, l2.next)。
14+
15+
```java
16+
/**
17+
* Definition for singly-linked list.
18+
* public class ListNode {
19+
* int val;
20+
* ListNode next;
21+
* ListNode(int x) { val = x; }
22+
* }
23+
*/
24+
class Solution {
25+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
26+
if (l1 == null) {
27+
return l2;
28+
}
29+
if (l2 == null) {
30+
return l1;
31+
}
32+
if (l1.val < l2.val) {
33+
l1.next = mergeTwoLists(l1.next, l2);
34+
return l1;
35+
}
36+
l2.next = mergeTwoLists(l1, l2.next);
37+
return l2;
38+
}
39+
}
40+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
3+
if (l1 == null) {
4+
return l2;
5+
}
6+
if (l2 == null) {
7+
return l1;
8+
}
9+
if (l1.val < l2.val) {
10+
l1.next = mergeTwoLists(l1.next, l2);
11+
return l1;
12+
}
13+
l2.next = mergeTwoLists(l1, l2.next);
14+
return l2;
15+
}
16+
}

solution/203.Remove Linked List Elements/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ class Solution {
3030
return head.val != val ? head : head.next;
3131
}
3232
}
33-
```
33+
```
34+

0 commit comments

Comments
 (0)