Title description
Input two monotonically increasing linked lists, and output the synthesized linked list of the two linked lists. Of course, we need the synthesized linked list to satisfy the monotonic non-decreasing rule.
Time limit: 1 second Space limit: 32768K Heat index: 556831
Knowledge points of this question: linked list
/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function Merge(pHead1, pHead2)
{
//任意链表为空则返回另一链表
if(pHead1 == null) return pHead2;
if(pHead2 == null) return pHead1;
//遍历两个两边,回调结点小的
if(pHead1.val > pHead2.val){
var res = pHead2;
res.next = Merge(pHead1, pHead2.next);
}else{
var res = pHead1;
res.next = Merge(pHead1.next, pHead2);
}
return res;
}