"Add Two Numbers" JavaScript Leetcode 错误
"Add Two Numbers" JavaScript Leetcode error
我的 leetcode 出现错误,我不确定为什么:
var addTwoNumbers = function(l1, l2) {
let newL1 = []
let newL2 = []
let answer = []
for(let i = 0; i < l1.length; i++) {
newL1[i] = l1[l1.length - 1 - i]
}
for(let i = 0; i < l2.length; i++) {
newL2[i] = l2[l2.length - 1 - i]
}
let num = parseInt(newL1.toString().replace(/,/g, '')) + parseInt(newL2.toString().replace(/,/g, ''))
let rawAnswer = (num.toString().split(""))
for(let i=0; i < rawAnswer.length; i++) {
answer[i] = parseInt(rawAnswer[i])
}
return answer
}
错误:
Line 45 in solution.js
throw new TypeError(__serialize__(ret) + " is not valid value for the expected return type ListNode");
^
TypeError: null is not valid value for the expected return type ListNode
Line 45: Char 20 in solution.js (Object.<anonymous>)
Line 16: Char 8 in runner.js (Object.runner)
Line 29: 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
挑战描述:
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
示例:
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.
我不确定为什么会出现此错误,但我知道我正在做一些 leetcode 不喜欢的事情。
谢谢
描述如下:return the sum as a linked list
你正在做两个 parseInt
和 return 求和(这是一个数字)但它应该 return 一个链接列表,由列表的头部定义(第一个 ListNode
对象。
var addTwoNumbers = function(l1, l2) {
let k=new ListNode(0,null);
let k1= k;
let x,y,c=0;
while(l1!=null || l2!=null){
x = l1!=null?l1.val:0;
y = l2!=null?l2.val:0;
c=x+y+c;
k1.next =new ListNode(c%10,null);
k1 = k1.next ;
c=parseInt(c/10);
if(l1!=null) l1=l1.next ;
if(l2!=null) l2=l2.next ;
}
if(c>0) {
k1.next=new ListNode(c,null);
k1=k1.next ;
}
return k.next;
};
ListNode这个函数.
- value.next - 下一项
- value.val - 该元素的值
我的 leetcode 出现错误,我不确定为什么:
var addTwoNumbers = function(l1, l2) {
let newL1 = []
let newL2 = []
let answer = []
for(let i = 0; i < l1.length; i++) {
newL1[i] = l1[l1.length - 1 - i]
}
for(let i = 0; i < l2.length; i++) {
newL2[i] = l2[l2.length - 1 - i]
}
let num = parseInt(newL1.toString().replace(/,/g, '')) + parseInt(newL2.toString().replace(/,/g, ''))
let rawAnswer = (num.toString().split(""))
for(let i=0; i < rawAnswer.length; i++) {
answer[i] = parseInt(rawAnswer[i])
}
return answer
}
错误:
Line 45 in solution.js
throw new TypeError(__serialize__(ret) + " is not valid value for the expected return type ListNode");
^
TypeError: null is not valid value for the expected return type ListNode
Line 45: Char 20 in solution.js (Object.<anonymous>)
Line 16: Char 8 in runner.js (Object.runner)
Line 29: 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
挑战描述:
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself.
示例:
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.
我不确定为什么会出现此错误,但我知道我正在做一些 leetcode 不喜欢的事情。
谢谢
描述如下:return the sum as a linked list
你正在做两个 parseInt
和 return 求和(这是一个数字)但它应该 return 一个链接列表,由列表的头部定义(第一个 ListNode
对象。
var addTwoNumbers = function(l1, l2) {
let k=new ListNode(0,null);
let k1= k;
let x,y,c=0;
while(l1!=null || l2!=null){
x = l1!=null?l1.val:0;
y = l2!=null?l2.val:0;
c=x+y+c;
k1.next =new ListNode(c%10,null);
k1 = k1.next ;
c=parseInt(c/10);
if(l1!=null) l1=l1.next ;
if(l2!=null) l2=l2.next ;
}
if(c>0) {
k1.next=new ListNode(c,null);
k1=k1.next ;
}
return k.next;
};
ListNode这个函数.
- value.next - 下一项
- value.val - 该元素的值