题目:
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
关键思路: 用一个变量承载当前节点
var mergeTwoLists = function (list1, list2) {
let newHead = {}; //初始化链表承载堆
let curNode = newHead; // 承载当前节点
while(list1 && list2) {
if(list1.val < list2.val) {
curNode.next = list1;
list1 = list1.next;
} else {
curNode.next = list2;
list2 = list2.next;
}
curNode = curNode.next;
}
if(list1) {
curNode.next = list1;
}
if(list2) {
curNode.next = list2;
}
return newHead.next;
}
———— 前端、Javascript实现、算法、刷题、leetcode