image_2021-11-14_00-55-39.png
49.4 KB
#N21. Merge Two Sorted Lists
problem link
#solution
problem link
#solution
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head = new ListNode();
ListNode temp=head;
while(l1!=null&&l2!=null){
if(l1.val>=l2.val){
temp.next=l2;
l2=l2.next;
}
else{
temp.next=l1;
l1=l1.next;
}
temp=temp.next;
}
temp.next=(l1==null)?l2:l1;
return head.next;
}
}