add: 合并两个有序链表

This commit is contained in:
2020-05-01 17:25:01 +08:00
parent ecab5b8850
commit b665d6595e
4 changed files with 73 additions and 3 deletions

View File

@ -0,0 +1,42 @@
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
export const mergeTwoLists = function (l1, l2) {
// src/list/merge-k-sorted-lists.js 中已经写过了
const dummyHead = {}
let current = dummyHead
while (l1 !== null && l2 !== null) {
if (l1.val < l2.val) {
if (l1.val !== null) {
current.next = l1
current = current.next
}
l1 = l1.next
} else {
if (l2.val !== null) {
current.next = l2
current = current.next
}
l2 = l2.next
}
}
if (l1 === null) {
current.next = l2
} else {
current.next = l1
}
return dummyHead.next
}