algorighm/top-interview-leetcode150/link/21合并两个有序链表.js

98 lines
3.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} list1
* @param {ListNode} list2
* @return {ListNode}
*/
const mergeTwoLists = function (list1, list2) {
};
function ListNode(val, next) {
this.val = (val === undefined ? 0 : val);
this.next = (next === undefined ? null : next);
}
/*
遍历两个链表如果l1的值小于l2的值就创建一个新的节点把l1的值放入这个新节点中之后把这个新节点
拼接到l3中之后返回代表l3的头节点即可
*/
function f1(list1, list2) {
const dummyHead = new ListNode(0); // 哑节点,用于拼接链表
let current = dummyHead;
while (list1 !== null || list2 !== null) {
if (list1 && list2) {
if (list1.val <= list2.val) {
current.next = new ListNode(list1.val);
list1 = list1.next;
} else {
current.next = new ListNode(list2.val);
list2 = list2.next;
}
current = current.next;
} else if (list1 === null) { // 当list1为空时直接将list2剩余部分拼接即可
current.next = list2;
break;
} else if (list2 === null) { // 当list2为空时直接将list1剩余部分拼接即可
current.next = list1;
break;
}
}
return dummyHead.next;
}
/*
思路和上面的一致,上面的效率之所以慢是因为花费了大量的时间在创建新节点上,这个操作虽然直观,但是
显得多余。
*/
function f2(list1, list2) {
const dummyHead = new ListNode(0); // 哑节点,用于拼接链表
let current = dummyHead;
while (list1 !== null && list2 !== null) {
if (list1.val <= list2.val) {
current.next = list1;
list1 = list1.next;
} else {
current.next = list2;
list2 = list2.next;
}
current = current.next;
}
// 如果 list1 或者 list2 还有剩余节点,直接拼接到结果链表
if (list1 !== null) {
current.next = list1;
} else if (list2 !== null) {
current.next = list2;
}
return dummyHead.next;
}
/*
利用递归思考递归过程首先思考f3的作用f3的作用非常单一就是合并两个有序链表合并完成之后使其
仍然有序假设list1[0]比list2[0]小那么list1[0]一定是合并后链表的头节点我们只需把list1[1:]和list2
继续做交给f3把合并后的头节点作为list1[0]的next,不就完成整个递归过程了嘛!
*/
function f3(l1, l2) {
if (l1 === null) {
return l2; // 如果 l1 空了,返回 l2
} if (l2 === null) {
return l1; // 如果 l2 空了,返回 l1
} if (l1.val < l2.val) {
// 如果 l1 的值小于 l2则 l1 当前节点连接到后面的结果
l1.next = mergeTwoLists(l1.next, l2);
return l1; // 返回 l1表示 l1 的节点被连接到了合并后的链表中
}
// 否则l2 当前节点连接到后面的结果
l2.next = mergeTwoLists(l1, l2.next);
return l2; // 返回 l2表示 l2 的节点被连接到了合并后的链表中
}