public ListNode mergeTwoLists(ListNode l1,ListNode l2){
if(l1null){
return l2;
}
if(l2null){
return l1;
}
ListNode newhead=null;
ListNode newTail=null;
ListNode cur1=l1;
ListNode cur2=l2;
while(cur1!=null&&cur2!=null){
if(cur1.val<cur2.val){
if(newTailnull){
newhead=cur1;
newTail=cur1;
}else{
newTail.next=cur1;
newTail=newTail.next;
}
cur1=cur1.next;
}else{
if(newTailnull){
newhead=cur2;
newTail=cur2;
}else{
newTail.next=cur2;
newTail=newTail.next;
}
cur2=cur2.next;
}
}
if(cur1==null){
newTail.next=cur2;
}else{
newTail.next=cur1;
}
return newhead;
}
来源:CSDN
作者:keithendnasjkf
链接:https://blog.csdn.net/keithendnasjkf/article/details/104678805