题解 | #合并两个排序的链表#
合并两个排序的链表
http://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337
function Merge(pHead1, pHead2){
let dummyHead = new ListNode(-1) //由于不确定第一个节点值的相对大小,所以创建一个虚拟头结点dummyHead
let prev = dummyHead //创建一个prev指针
let cur1 = pHead1, cur2 = pHead2
while(cur1 && cur2){
if(cur1.val >= cur2.val){
prev.next = cur2
cur2 = cur2.next;
}else{
prev.next = cur1;
cur1 = cur1.next;
}
prev = prev.next;
}
if(cur1){
prev.next = cur1;
}else{
prev.next = cur2
}
return dummyHead.next
}