/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode newList = new ListNode(0);
newList.next = null;
ListNode head = newList;
while(l1 != null && l2 != null){
if(l1.val <= l2.val){
newList.next = l1;
newList = newList.next;
l1 = l1.next;
}
else{
newList.next = l2;
newList = newList.next;
l2 = l2.next;
}
}
if(l1 != null){
newList.next = l1;
}
if(l2 != null){
newList.next = l2;
}
return head.next;
}
}
Merge Two Sorted Lists
最新推荐文章于 2024-01-12 14:30:52 发布