AcWing 36. 合并两个排序的链表-Java
原题链接
简单
作者:
Fant.J
,
2019-05-16 11:23:54
,
所有人可见
,
阅读 1048
class Solution {
public ListNode merge(ListNode l1, ListNode l2) {
if(l1 == null && l2 == null){return null;}
ListNode result = new ListNode(-1);
ListNode temp = result;
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;
}
while(l1 != null){
temp.next = l1;
l1 = l1.next;
temp = temp.next;
}
while(l2 != null){
temp.next = l2;
l2 = l2.next;
temp = temp.next;
}
return result.next;
}
}