以后,再也不要为了剩一点点空间而把问题复杂化!就再建一个链表就完了,别整那些花里胡哨。
/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function Merge(pHead1, pHead2)
{
let head=new ListNode(-1); //勿直接赋值为null
let res=head; //勿漏!!!
while(pHead1!==null&&pHead2!==null){
if(pHead1.val<=pHead2.val){
head.next=pHead1;
pHead1=pHead1.next;
head=head.next;//勿漏
}else{
head.next=pHead2;
pHead2=pHead2.next;
head=head.next;//勿漏
}
}
if(pHead1===null) head.next=pHead2;
else head.next=pHead1;
return res.next;
}