I have been working on a leetcode problem and I believe I have found the solution. However, there seems to be a discrepancy in my function's output - it returns an Array whereas the problem specifically requires a NodeList.
I am struggling to figure out how to either create a NodeList without involving the DOM or convert my existing Array into a NodeList.
Here is the problem description:
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
Below is my code snippet :
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);
--------UPDATE--------- I now realize that my misunderstanding of the problem was due to overlooking the fact that it pertains to Linked Lists. Thank you for pointing that out.