Skip to content

Commit b2bd355

Browse files
committed
feat: add 021
1 parent 4849be7 commit b2bd355

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
|14|[Longest Common Prefix][014]|String|
1515
|19|[Remove Nth Node From End of List][019]|Linked List, Two Pointers|
1616
|20|[Valid Parentheses][020]|Stack, String|
17+
|21|[Merge Two Sorted Lists][021]|Linked List|
1718

1819

1920
## Medium
@@ -42,3 +43,4 @@
4243
[014]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/014/README.md
4344
[019]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/019/README.md
4445
[020]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/020/README.md
46+
[021]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/021/README.md

note/021/README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
**Tags:** Linked List
8+
9+
## 思路
10+
11+
题意是用一个新链表来合并两个已排序的链表,那我们只需要从头开始比较已排序的两个链表,新链表指针每次指向值小的节点,依次比较下去,最后,当其中一个链表到达了末尾,我们只需要把新链表指针指向另一个没有到末尾的链表此时的指针即可。
12+
13+
``` java
14+
/**
15+
* Definition for singly-linked list.
16+
* public class ListNode {
17+
* int val;
18+
* ListNode next;
19+
* ListNode(int x) { val = x; }
20+
* }
21+
*/
22+
public class Solution {
23+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
24+
ListNode head = new ListNode(0);
25+
ListNode temp = head;
26+
while (l1 != null && l2 != null) {
27+
if (l1.val < l2.val) {
28+
temp.next = l1;
29+
l1 = l1.next;
30+
} else {
31+
temp.next = l2;
32+
l2 = l2.next;
33+
}
34+
temp = temp.next;
35+
}
36+
temp.next = l1 != null ? l1 : l2;
37+
return head.next;
38+
}
39+
}
40+
```
41+
42+
43+
## 结语
44+
45+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
46+
47+
48+
49+
[title]: https://leetcode.com/problems/merge-two-sorted-lists
50+
[ajl]: https://github.com/Blankj/awesome-java-leetcode
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.blankj.easy._021;
2+
3+
/**
4+
* <pre>
5+
* author: Blankj
6+
* blog : http://blankj.com
7+
* time : 2017/04/21
8+
* desc :
9+
* </pre>
10+
*/
11+
12+
public class Solution {
13+
14+
static class ListNode {
15+
int val;
16+
ListNode next;
17+
18+
ListNode(int x) {
19+
val = x;
20+
}
21+
22+
@Override
23+
public String toString() {
24+
String str = "[" + String.valueOf(val);
25+
ListNode p = next;
26+
while (p != null) {
27+
str += ", " + String.valueOf(p.val);
28+
p = p.next;
29+
}
30+
return str + "]";
31+
}
32+
}
33+
34+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
35+
ListNode head = new ListNode(0);
36+
ListNode temp = head;
37+
while (l1 != null && l2 != null) {
38+
if (l1.val < l2.val) {
39+
temp.next = l1;
40+
l1 = l1.next;
41+
} else {
42+
temp.next = l2;
43+
l2 = l2.next;
44+
}
45+
temp = temp.next;
46+
}
47+
temp.next = l1 != null ? l1 : l2;
48+
return head.next;
49+
}
50+
51+
public static void main(String[] args) {
52+
Solution solution = new Solution();
53+
ListNode listNode00 = new ListNode(1);
54+
ListNode listNode01 = new ListNode(3);
55+
ListNode listNode02 = new ListNode(5);
56+
ListNode listNode03 = new ListNode(7);
57+
ListNode listNode04 = new ListNode(9);
58+
listNode00.next = listNode01;
59+
listNode01.next = listNode02;
60+
listNode02.next = listNode03;
61+
listNode03.next = listNode04;
62+
listNode04.next = null;
63+
ListNode listNode10 = new ListNode(2);
64+
ListNode listNode11 = new ListNode(4);
65+
ListNode listNode12 = new ListNode(6);
66+
ListNode listNode13 = new ListNode(8);
67+
ListNode listNode14 = new ListNode(10);
68+
listNode10.next = listNode11;
69+
listNode11.next = listNode12;
70+
listNode12.next = listNode13;
71+
listNode13.next = listNode14;
72+
listNode14.next = null;
73+
System.out.println(listNode00.toString());
74+
System.out.println(listNode10.toString());
75+
System.out.println(solution.mergeTwoLists(listNode00, listNode10).toString());
76+
}
77+
}

0 commit comments

Comments
 (0)