Java教程

BM4 合并两个排序的链表

本文主要是介绍BM4 合并两个排序的链表,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

 

 

 

 

 

 

 

function ListNode(x){
    this.val = x;
    this.next = null;
}
function Merge(pHead1, pHead2)
{
     // write code here
    let cur = new ListNode()
    let dummy = cur
    while(pHead1 && pHead2){
        if(pHead1.val <= pHead2.val) {
            cur.next = pHead1
            pHead1 = pHead1.next
        } else {
            cur.next = pHead2
            pHead2 = pHead2.next
        }
        cur = cur.next
    }
    cur.next = pHead1 ? pHead1 : pHead2
    return dummy.next
}
module.exports = {
    Merge : Merge
};

  

这篇关于BM4 合并两个排序的链表的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!