-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMergeTwoSortedLists.java
More file actions
133 lines (120 loc) · 3.39 KB
/
Copy pathMergeTwoSortedLists.java
File metadata and controls
133 lines (120 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* <p>将两个升序链表合并为一个新的 <strong>升序</strong> 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 </p>
*
* <p> </p>
*
* <p><strong>示例 1:</strong></p>
* <img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/merge_ex1.jpg" style="width: 662px; height: 302px;" />
* <pre>
* <strong>输入:</strong>l1 = [1,2,4], l2 = [1,3,4]
* <strong>输出:</strong>[1,1,2,3,4,4]
* </pre>
*
* <p><strong>示例 2:</strong></p>
*
* <pre>
* <strong>输入:</strong>l1 = [], l2 = []
* <strong>输出:</strong>[]
* </pre>
*
* <p><strong>示例 3:</strong></p>
*
* <pre>
* <strong>输入:</strong>l1 = [], l2 = [0]
* <strong>输出:</strong>[0]
* </pre>
*
* <p> </p>
*
* <p><strong>提示:</strong></p>
*
* <ul>
* <li>两个链表的节点数目范围是 <code>[0, 50]</code></li>
* <li><code>-100 <= Node.val <= 100</code></li>
* <li><code>l1</code> 和 <code>l2</code> 均按 <strong>非递减顺序</strong> 排列</li>
* </ul>
* <div><div>Related Topics</div><div><li>递归</li><li>链表</li></div></div><br><div><li>👍 2025</li><li>👎 0</li></div>
*/
package leetcode1;
public class MergeTwoSortedLists {
public static void main(String[] args) {
Solution solution = new MergeTwoSortedLists().new Solution();
}
public class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
/**
* 循环
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode node = new ListNode(0);
ListNode prev = node;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
node.next = l1;
l1 = l1.next;
} else {
node.next = l2;
l2 = l2.next;
}
node = node.next;
}
node.next = l1 == null ? l2 : l1;
return prev.next;
}
}
/**
* 递归
*/
class Solution2 {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) return l2;
if (l2 == null) return l1;
if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else {
l2.next = mergeTwoLists(l2.next, l1);
return l2;
}
}
}
/**
* 循环第二种写法
*/
class Solution3 {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode node = new ListNode(0);
ListNode curr = node;
while (true) {
if (l1 == null) {
curr.next = l2;
return node.next;
}
if (l2 == null) {
curr.next = l1;
return node.next;
}
if (l1.val < l2.val) {
curr.next = l1;
l1 = l1.next;
} else {
curr.next = l2;
l2 = l2.next;
}
curr = curr.next;
}
}
}
}