Skip to content

Commit 15f2634

Browse files
author
vesper
committed
feat: add bytedance.linked_list_and_tree.merge_two_sorted_lists
1 parent 66515bd commit 15f2634

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@
115115

116116
- 【专项】
117117
- 【ByteDance】
118-
- [无重复字符的最长子串](./src/leetcode/specialized/bytedance/longest_substring_without_repeating_characters/Solution.java)
118+
- 【字符串】
119+
- [无重复字符的最长子串](src/leetcode/specialized/bytedance/string/longest_substring_without_repeating_characters/Solution.java)
120+
- 【链表与树】
121+
- [合并两个有序链表](./src/leetcode/specialized/bytedance/linked_list_and_tree/merge_two_sorted_lists/Solution.java)
119122

120123
#### LeetCode 周赛
121124

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package leetcode.specialized.bytedance.linked_list_and_tree.merge_two_sorted_lists;
2+
3+
import leetcode.list.base.ListNode;
4+
5+
public class Solution {
6+
/*
7+
* 合并两个有序链表
8+
* link: https://leetcode-cn.com/explore/interview/card/bytedance/244/linked-list-and-tree/1048/
9+
* Label: 链表
10+
*
11+
* 执行用时 : 1 ms
12+
* 内存消耗 : 39.3 MB
13+
* */
14+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
15+
ListNode res = new ListNode(-1);
16+
ListNode p1 = l1;
17+
ListNode p2 = l2;
18+
ListNode c = res;
19+
while (p1 != null && p2 != null) {
20+
if (p1.val <= p2.val) {
21+
c.next = p1;
22+
c = c.next;
23+
p1 = p1.next;
24+
} else {
25+
c.next = p2;
26+
c = c.next;
27+
p2 = p2.next;
28+
}
29+
}
30+
if (p1 == null) {
31+
c.next = p2;
32+
} else {
33+
c.next = p1;
34+
}
35+
return res.next;
36+
}
37+
38+
public static void main(String[] args) {
39+
Solution ss = new Solution();
40+
System.out.println(ss.mergeTwoLists(new ListNode(new int[]{1, 2, 4}), new ListNode(new int[]{1, 3, 4})));
41+
System.out.println(ss.mergeTwoLists(new ListNode(new int[]{1}), new ListNode(new int[]{1})));
42+
System.out.println(ss.mergeTwoLists(null, new ListNode(new int[]{1})));
43+
System.out.println(ss.mergeTwoLists(new ListNode(new int[]{1}), null));
44+
System.out.println(ss.mergeTwoLists(null, null));
45+
}
46+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package leetcode.specialized.bytedance.string.longest_common_prefix;
2+
3+
public class Solution {
4+
public String longestCommonPrefix(String[] strs) {
5+
return null;
6+
}
7+
}

src/leetcode/specialized/bytedance/longest_substring_without_repeating_characters/Solution.java renamed to src/leetcode/specialized/bytedance/string/longest_substring_without_repeating_characters/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package leetcode.specialized.bytedance.longest_substring_without_repeating_characters;
1+
package leetcode.specialized.bytedance.string.longest_substring_without_repeating_characters;
22

33
import java.util.HashMap;
44
import java.util.Map;

0 commit comments

Comments
 (0)