const addTwoNumbers = (l1, l2) => {
let res = [],
max = Math.max(l1.length, l2.length),
sum = [],
bool = false;
for (let i = 1; i <= max; i++) {
const digit = (l1[l1.length - i] | 0) + (l2[l2.length - i] | 0) + bool;
bool = digit > 9;
res.unshift(digit % 10);
}
if (bool) {
res.unshift(1);
}
for(let i = res.length - 1; i >= 0; i--) {
sum.push(res[i])
}
return sum;
};
console.log(addTwoNumbers([9,9,9,9], [9,9,9]));
Line 36 in solution.js
throw new TypeError(__serialize__(ret) + " is not valid value for the expected return type ListNode");
^
TypeError: [] is not valid value for the expected return type ListNode
Line 36: Char 20 in solution.js (Object.<anonymous>)
Line 16: Char 8 in runner.js (Object.runner)
Line 20: Char 26 in solution.js (Object.<anonymous>)
Line 1251: Char 30 in loader.js (Module._compile)
Line 1272: Char 10 in loader.js (Object.Module._extensions..js)
Line 1100: Char 32 in loader.js (Module.load)
Line 962: Char 14 in loader.js (Function.Module._load)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
Line 17: Char 47 in run_main_module.js
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var mergeTwoLists = function(l1, l2) {
var dummy = {
val : -1,
next : null
};
var curr = dummy;
while (l1 && l2) {
if (l1.val > l2.val) {
curr.next = l2;
l2 = l2.next;
} else {
curr.next = l1;
l1 = l1.next;
}
curr = curr.next;
}
curr.next = l1 || l2;
return dummy.next;
};