Skip to content

Commit f19a09f

Browse files
committed
add 021
1 parent 51ab802 commit f19a09f

File tree

4 files changed

+205
-0
lines changed

4 files changed

+205
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
| 237 | [Delete Node in a Linked List][237] |
4949
| 19 | [Remove Nth Node From End of List][019] |
5050
| 206 | [Reverse Linked List][206] |
51+
| 21 | [Merge Two Sorted Lists][021] |
52+
5153

5254

5355
**其他**
@@ -86,3 +88,4 @@
8688
[237]: https://github.com/andavid/leetcode-java/blob/master/note/237/README.md
8789
[019]: https://github.com/andavid/leetcode-java/blob/master/note/019/README.md
8890
[206]: https://github.com/andavid/leetcode-java/blob/master/note/206/README.md
91+
[021]: https://github.com/andavid/leetcode-java/blob/master/note/021/README.md

note/021/README.md

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# [Merge Two Sorted Lists][title]
2+
3+
## Description
4+
5+
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+
**Example:**
8+
9+
Input: 1->2->4, 1->3->4
10+
Output: 1->1->2->3->4->4
11+
12+
## 思路一
13+
14+
遍历两个链表,比较两个链表节点的大小,修改当前节点使其指向较小的那个节点。
15+
16+
## [完整代码][src]
17+
18+
```java
19+
/**
20+
* Definition for singly-linked list.
21+
* public class ListNode {
22+
* int val;
23+
* ListNode next;
24+
* ListNode(int x) { val = x; }
25+
* }
26+
*/
27+
class Solution {
28+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
29+
if (l1 == null) return l2;
30+
if (l2 == null) return l1;
31+
32+
ListNode dummy = new ListNode(-1);
33+
ListNode curr = dummy;
34+
35+
while (l1 != null && l2 != null) {
36+
if (l1.val <= l2.val) {
37+
curr.next = l1;
38+
l1 = l1.next;
39+
} else {
40+
curr.next = l2;
41+
l2 = l2.next;
42+
}
43+
curr = curr.next;
44+
}
45+
46+
if (l1 != null) {
47+
curr.next = l1;
48+
}
49+
50+
if (l2 != null) {
51+
curr.next = l2;
52+
}
53+
54+
return dummy.next;
55+
}
56+
}
57+
```
58+
59+
## 思路二
60+
61+
递归。
62+
63+
比较两个链表头节点的大小,指向较小的节点,并且该节点的 next 节点可以通过递归的方法得到。
64+
65+
## [完整代码][src2]
66+
67+
```java
68+
/**
69+
* Definition for singly-linked list.
70+
* public class ListNode {
71+
* int val;
72+
* ListNode next;
73+
* ListNode(int x) { val = x; }
74+
* }
75+
*/
76+
class Solution {
77+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
78+
if (l1 == null) return l2;
79+
if (l2 == null) return l1;
80+
81+
if (l1.val < l2.val) {
82+
l1.next = mergeTwoLists(l1.next, l2);
83+
return l1;
84+
} else {
85+
l2.next = mergeTwoLists(l2.next, l1);
86+
return l2;
87+
}
88+
}
89+
}
90+
```
91+
92+
[title]: https://leetcode.com/problems/merge-two-sorted-lists
93+
[src]: https://github.com/andavid/leetcode-java/blob/master/src/com/andavid/leetcode/_021/Solution.java
94+
[src2]: https://github.com/andavid/leetcode-java/blob/master/src/com/andavid/leetcode/_021/Solution2.java
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
class Solution {
2+
3+
public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
4+
if (l1 == null) return l2;
5+
if (l2 == null) return l1;
6+
7+
ListNode dummy = new ListNode(-1);
8+
ListNode curr = dummy;
9+
10+
while (l1 != null && l2 != null) {
11+
if (l1.val <= l2.val) {
12+
curr.next = l1;
13+
l1 = l1.next;
14+
} else {
15+
curr.next = l2;
16+
l2 = l2.next;
17+
}
18+
curr = curr.next;
19+
}
20+
21+
if (l1 != null) {
22+
curr.next = l1;
23+
}
24+
25+
if (l2 != null) {
26+
curr.next = l2;
27+
}
28+
29+
return dummy.next;
30+
}
31+
32+
public static class ListNode {
33+
int val;
34+
ListNode next;
35+
ListNode(int x) {
36+
val = x;
37+
}
38+
}
39+
40+
public static void main(String[] args) {
41+
ListNode l11 = new ListNode(1);
42+
ListNode l12 = new ListNode(2);
43+
ListNode l13 = new ListNode(4);
44+
ListNode l21 = new ListNode(1);
45+
ListNode l22 = new ListNode(3);
46+
ListNode l23 = new ListNode(4);
47+
l11.next = l12;
48+
l12.next = l13;
49+
l13.next = null;
50+
l21.next = l22;
51+
l22.next = l23;
52+
l23.next = null;
53+
54+
ListNode node = mergeTwoLists(l11, l21);
55+
while(node.next != null) {
56+
System.out.print(node.val + ",");
57+
node = node.next;
58+
}
59+
System.out.print(node.val);
60+
}
61+
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution2 {
2+
3+
public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
4+
if (l1 == null) return l2;
5+
if (l2 == null) return l1;
6+
7+
if (l1.val < l2.val) {
8+
l1.next = mergeTwoLists(l1.next, l2);
9+
return l1;
10+
} else {
11+
l2.next = mergeTwoLists(l2.next, l1);
12+
return l2;
13+
}
14+
}
15+
16+
public static class ListNode {
17+
int val;
18+
ListNode next;
19+
ListNode(int x) {
20+
val = x;
21+
}
22+
}
23+
24+
public static void main(String[] args) {
25+
ListNode l11 = new ListNode(1);
26+
ListNode l12 = new ListNode(2);
27+
ListNode l13 = new ListNode(4);
28+
ListNode l21 = new ListNode(1);
29+
ListNode l22 = new ListNode(3);
30+
ListNode l23 = new ListNode(4);
31+
l11.next = l12;
32+
l12.next = l13;
33+
l13.next = null;
34+
l21.next = l22;
35+
l22.next = l23;
36+
l23.next = null;
37+
38+
ListNode node = mergeTwoLists(l11, l21);
39+
while(node.next != null) {
40+
System.out.print(node.val + ",");
41+
node = node.next;
42+
}
43+
System.out.print(node.val);
44+
}
45+
46+
}

0 commit comments

Comments
 (0)