21. Merge Two Sorted Lists
程序员文章站
2022-06-02 13:41:52
...
import jdk.management.resource.internal.inst.AbstractPlainDatagramSocketImplRMHooks;
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode listNode = new ListNode(0);
ListNode tmp = listNode;
while (true) {
if(l1==null) {
if(l2==null) return tmp.next;
listNode.next = l2;
return tmp.next;
} else {
if(l2 ==null) {
listNode.next = l1;
return tmp.next;
} else {
if(l1.val >= l2.val) {
listNode.next = l2;
listNode = listNode.next;
l2 = l2.next;
} else {
listNode.next = l1;
listNode = listNode.next;
l1 = l1.next;
}
}
}
}
}
// public static void main(String[] args) {
// ListNode node1 = new ListNode(1);
// ListNode node3 = new ListNode(10);
// node1.next = node3;
// ListNode node2 = new ListNode(2);
// ListNode node4 = new ListNode(4);
// node2.next = node4;
// ListNode node = (new Solution()).mergeTwoLists(node1, node2);
// while (node != null) {
// System.out.print(node.val + " ");
// node = node.next;
// }
// }
}
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
推荐阅读
-
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
-
[LeetCode]Merge Two Sorted Lists(Python)
-
1.Merge Two Sorted Arrays. py/合并排序有序数列
-
Leetcode【121】 Merge Two Sorted Lists(Java版)-附测试代码
-
21. Merge Two Sorted Lists
-
merge-two-sorted-lists
-
21. Merge Two Sorted Lists
-
21. Merge Two Sorted Lists (E)
-
Merge Two Sorted Lists(C++)
-
21.Merge Two Sorted Lists