Javascript 合并两个列表LeetCode
我已经在Repl.it网站上解决了这个问题,但是当我在LeetCode上提交代码时,它给出了一个typeError,我将把它粘贴到这里:Javascript 合并两个列表LeetCode,javascript,node.js,c,c++,java,c#,python,go,rust,Javascript,Node.js,C,C++,Java,C#,Python,Go,Rust,我已经在Repl.it网站上解决了这个问题,但是当我在LeetCode上提交代码时,它给出了一个typeError,我将把它粘贴到这里: Line 29 in solution.js throw new TypeError(__serialize__(ret) + " is not valid value for the expected return type ListNode"); ^ TypeError: [] is not val
Line 29 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 29: Char 20 in solution.js (Object.<anonymous>)
Line 16: Char 8 in runner.js (Object.runner)
Line 13: Char 26 in solution.js (Object.<anonymous>)
Line 1200: Char 30 in loader.js (Module._compile)
Line 1220: Char 10 in loader.js (Object.Module._extensions..js)
Line 1049: Char 32 in loader.js (Module.load)
Line 937: Char 14 in loader.js (Function.Module._load)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
Line 17: Char 47 in run_main_module.js
solution.js中的第29行
throw new TypeError(_serialize__(ret)+)”不是预期返回类型的有效值
列表节点);
^
TypeError:[]不是预期返回类型ListNode的有效值
第29行:solution.js(对象)中的Char 20
第16行:runner.js(Object.runner)中的字符8
第13行:solution.js(对象)中的Char 26
第1200行:loader.js(Module.\u compile)中的Char 30
第1220行:loader.js(Object.Module.\u extensions..js)中的字符10
第1049行:loader.js(Module.load)中的字符32
第937行:loader.js(Function.Module.\u load)中的字符14
在Function.executeUserEntryPoint[作为runMain](internal/modules/run_main.js:71:12)
第17行:run_main_module.js中的字符47
下面是代码:
var mergeTwoLists = function(l1, l2) {
let i = 0, j = 0;
var out = [];
while(i < l1.length || j < l2.length) {
if(j == l2.length || i < l1.length && l1[i] < l2[j]) {
out.push(l1[i++]);
} else {
out.push(l2[j++]);
}
}
return out;
};
var mergetwolist=函数(l1、l2){
设i=0,j=0;
var out=[];
而(i
我真的不知道问题出在哪里。。。如果有人能帮忙,我将不胜感激这是一个合并问题,而不是常规数组合并。这将通过:
/**
* 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;
};
您的列表如下所示:
/**
* 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}
*/
工具书类
- 有关其他详细信息,请参见。这里有很多公认的解决方案,有各种解释和解释,有高效的算法,还有渐近/复杂性分析
- 我们会根据标准和惯例(例如,,,,,,,,)编写代码