Leetcode题解之 —— 合并两个有序列表
思路
js原生不具有LinkList
, 使用Array
实现
题解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
var mergeTwoLists = function(l1, l2) { const cache = [];
while (l1) { cache.push(new ListNode(l1.val)); l1 = l1.next; } while (l2) { cache.push(new ListNode(l2.val)); l2 = l2.next; }
cache.sort((a, b) => a.val - b.val);
let [count, len] = [0, cache.length];
if (len) { while(count < len - 1){ cache[count].next = cache[count + 1]; count++; } return cache[0]; }
return null; };
|