File tree 5 files changed +61
-3
lines changed
021.Merge Two Sorted Lists
203.Remove Linked List Elements
5 files changed +61
-3
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ Complete solutions to Leetcode problems, updated daily.
13
13
| # | Title | Tags |
14
14
| ---| ---| ---|
15
15
| 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 ` |
16
17
| 203 | [ Remove Linked List Elements] ( https://github.com/yanglbme/leetcode/tree/master/solution/203.Remove%20Linked%20List%20Elements ) | ` Linked List ` |
17
18
18
19
### Medium
@@ -33,7 +34,7 @@ I'm looking for long-term contributors/partners to this repo! Send me PRs if you
33
34
## Contributors
34
35
35
36
<!-- 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
+ | ---| ---|
38
39
39
40
<!-- ALL-CONTRIBUTORS-LIST:END -->
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -30,4 +30,5 @@ class Solution {
30
30
return head. val != val ? head : head. next;
31
31
}
32
32
}
33
- ```
33
+ ```
34
+
File renamed without changes.
You can’t perform that action at this time.
0 commit comments