这个题啊,做过n遍了,知道怎么做,但每次都有点问题。
我想说
你是怎么想的创建一个空节点空节点怎么可能有值呢,你放个值进去啊!
肯定是两个都不为空啊,再出来判断
当然,最后返回的是next哦
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode pre=new ListNode(-1);
ListNode cur=pre;
while(l1!=null&&l2!=null){
if(l1.val>l2.val){
cur.next=l2;
l2=l2.next;
}else{
cur.next=l1;
l1=l1.next;
}
cur=cur.next;
}
if(l1==null)cur.next=l2;
if(l2==null)cur.next=l1;
return pre.next;
}