用js刷剑指offer(合并两个排序的链表)
<h2 id="toc_0">题目描述</h2><p>输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。</p>
<p>牛客网链接</p>
<h2 id="toc_1">js代码</h2>
<pre><code class="language-javascript">/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function Merge(pHead1, pHead2)
{
// write code here
if (!pHead1) return pHead2
if (!pHead2) return pHead1
let now = new ListNode(0)
const root = now
while (pHead1 && pHead2){
if (pHead1.val > pHead2.val){
now.next = pHead2
now = now.next
pHead2 = pHead2.next
}else{
now.next = pHead1
now = now.next
pHead1 = pHead1.next
}
}
if (pHead1) {
now.next = pHead1
}else{
now.next = pHead2
}
return root.next
}
</code></pre><br><br>
来源:https://www.cnblogs.com/dpnlp/p/yongjs-shua-jian-zhioffer-he-bing-liang-ge-pai-xu-.html
頁:
[1]