[英]is it possible to convert an Array to a NodeList in javascript? leetcode problem --Linked lists--
I am trying to solve a leetcode problem and I think I have figured it out, the problem is that my function returns an Array with the correct Answer and the problem specifies that I must return a NodeList,我正在尝试解决一个 leetcode 问题,我想我已经弄清楚了,问题是我的 function 返回了一个带有正确答案的数组,问题指定我必须返回一个 NodeList,
I can't figure out how to create a NodeList without reaching to the DOM, or to transform my Array to NodeList.我无法弄清楚如何在不访问 DOM 的情况下创建 NodeList,或者将我的 Array 转换为 NodeList。
The problem is:问题是:
Merge two sorted linked lists and return it as a new sorted list.
合并两个排序的链表并将其作为新的排序列表返回。 The new list should be made by splicing together the nodes of the first two lists.
应该通过将前两个列表的节点拼接在一起来制作新列表。
Example:
例子:
Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4
输入:1->2->4、1->3->4 Output:1->1->2->3->4->4
my code is:我的代码是:
const listOne = [1, 2, 4];
const listTwo = [1, 3, 4];
function myFunction(l1, l2) {
let lslength;
let newList = [];
if (l1.length >= l2.length) {
lslength = l1.length;
} else {
lslength = l2.length;
}
for (let i = 0; i < lslength; i++) {
if (l1[i] === l2[i]) {
newList.push(l1[i]);
newList.push(l2[i]);
} else if (l1[i] < l2[i]) {
newList.push(l1[i]);
newList.push(l2[i]);
} else if (l1[i] > l2[i]) {
newList.push(l2[i]);
newList.push(l1[i]);
} else if (l1[i]) {
newList.push(l1[i]);
} else {
newList.push(l2[i]);
}
}
return newList;
}
myFunction(listOne, listTwo);
--------EDIT--------- ok so I really didnt understand the problem because its about Linked Lists, now I know, thank you --------编辑---------好的,所以我真的不明白这个问题,因为它是关于链接列表的,现在我知道了,谢谢
You don't need to do that.你不需要这样做。
The test cases are represented like arrays for user simplicity I guess.我猜为了用户简单,测试用例表示为 arrays。 Though, LinkedList and array are two different data types.
不过, LinkedList和数组是两种不同的数据类型。 Your function is returning an array, which is not a desired output here.
您的 function 正在返回一个数组,这在此处不是所需的 output。 The desired output is simply a merged linked list, not a merged array.
所需的 output 只是一个合并的链表,而不是合并的数组。
Here we'd use a sentinel node to merge two linked lists.在这里,我们将使用哨兵节点来合并两个链表。 This'll get accepted:
这将被接受:
/**
* 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 sentinel = {
val: -1,
next: null
};
var curr = sentinel;
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 sentinel.next;
};
For additional details, you can see the Discussion Board .有关其他详细信息,您可以查看讨论板。 There are plenty of accepted solutions with a variety of languages and explanations, efficient algorithms, as well as asymptotic time / space complexity analysis 1 , 2 in there.
有很多公认的解决方案,其中包含多种语言和解释、高效算法以及渐近时间/空间复杂度分析1 、 2 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.