Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,207,106 members, 7,997,869 topics. Date: Friday, 08 November 2024 at 07:32 PM

Leetcode Help - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Leetcode Help (941 Views)

Solving Leetcode Daily. / Leetcode Solution Error Please Help Its Just Driving Me Crazy / Free Leetcode Questions (2) (3) (4)

(1) (Reply) (Go Down)

Leetcode Help by efelico: 1:40am On Aug 23, 2022
var mergeTwoLists = function(list1, list2) {
let l1,l2,l3,head3;
[l1, l2, head3] = [list1, list2, new ListNode(0)];
l3 = head3;
while(l1 && l2) {
if( l1.val >= l2.val ) {
l3.next = l2;
l2 = l2.next;
}else {
l3.next = l1;
l1 = l1.next;
}
l3 = l3.next;
}
l3.next = l1 || l2;
return head3.next;
};
mergeTwoLists([1,2,4],[1,3,4]);


I noticed this javascript code produces "Uncaught ReferenceError: ListNode is not defined at mergeTwoLists " which is coming from the third line when I try running it on my text editor but run's well on leetcode javascript editor
1. Please I want to know why the code is working on leetcode text editor and not working on the usual text editor.
2. Please I also need explanation on how l1.val, l2.val,l3.next,l2.next and l1.next properties are not returning un define. Because to the best of my understanding regular array does not have those value

Re: Leetcode Help by ojelabi30(m): 3:41am On Aug 23, 2022
try paste this on stackoverflow for quick solutions
Re: Leetcode Help by Nobody: 8:19am On Aug 23, 2022
ListNode should be defined by LeetCode, there is debug code in playground button where the ListNode is defined by LeetCode

CC: Stackoverflow
Re: Leetcode Help by tensazangetsu20(m): 9:23am On Aug 23, 2022
It is not defined because you haven't created the linked list data structure. You are running it outside leetcode which gives you the data structure defined.

1 Like

Re: Leetcode Help by chukwuebuka65(m): 9:57am On Aug 23, 2022
efelico:
var mergeTwoLists = function(list1, list2) {
let l1,l2,l3,head3;
[l1, l2, head3] = [list1, list2, new ListNode(0)];
l3 = head3;
while(l1 && l2) {
if( l1.val >= l2.val ) {
l3.next = l2;
l2 = l2.next;
}else {
l3.next = l1;
l1 = l1.next;
}
l3 = l3.next;
}
l3.next = l1 || l2;
return head3.next;
};
mergeTwoLists([1,2,4],[1,3,4]);


I noticed this javascript code produces "Uncaught ReferenceError: ListNode is not defined at mergeTwoLists " which is coming from the third line when I try running it on my text editor but run's well on leetcode javascript editor
1. Please I want to know why the code is working on leetcode text editor and not working on the usual text editor.
2. Please I also need explanation on how l1.val, l2.val,l3.next,l2.next and l1.next properties are not returning un define. Because to the best of my understanding regular array does not have those value

Try to study and understand the code first.Where is your ListNode class? So u dont know the “new list ode(0)” is instance of a class called list node which might be an implementation of a linked list. So where is the class?
Re: Leetcode Help by Nobody: 10:24am On Aug 23, 2022
chukwuebuka65:


Try to study and understand the code first.Where is your ListNode class? So u dont know the “new list ode(0)” is instance of a class called list node which might be an implementation of a linked list. So where is the class?

Probably a copy paste solution. grin
Re: Leetcode Help by chukwuebuka65(m): 10:32am On Aug 23, 2022
GREATIGBOMAN:


Probably a copy paste solution. grin

I thought as much.
Re: Leetcode Help by eaimiesylv(m): 10:40am On Aug 23, 2022
tensazangetsu20:
It is not defined because you haven't created the linked list data structure. You are running it outside leetcode which gives you the data structure defined.

Thank you
Re: Leetcode Help by eaimiesylv(m): 10:44am On Aug 23, 2022
chukwuebuka65:


Try to study and understand the code first.Where is your ListNode class? So u dont know the “new list node(0)” is instance of a class called list node which might be an implementation of a linked list. So where is the class?


I actually copied the code. What I am saying is that for the exact code to run on leetcode and not run on an ide is something else. This same code is working without listnode class being define on leetcode that is why I was asking.


However Tensa comment has helped to clarify me
tensazangetsu20:
It is not defined because you haven't created the linked list data structure. You are running it outside leetcode which gives you the data structure defined.
.

Re: Leetcode Help by chukwuebuka65(m): 11:41am On Aug 23, 2022
eaimiesylv:



I actually copied the code. What I am saying is that for the exact code to run on leetcode and not run on an ide is something else. This same code is working without listnode class being define on leetcode that is why I was asking.


However Tensa comment has helped to clarify me
.

Ok
Re: Leetcode Help by LikeAking: 5:33pm On Aug 23, 2022
I have solved this one bf.

I used shift and pop and a while loop.
Re: Leetcode Help by chukwuebuka65(m): 6:09pm On Aug 23, 2022
efelico:
var mergeTwoLists = function(list1, list2) {
let l1,l2,l3,head3;
[l1, l2, head3] = [list1, list2, new ListNode(0)];
l3 = head3;
while(l1 && l2) {
if( l1.val >= l2.val ) {
l3.next = l2;
l2 = l2.next;
}else {
l3.next = l1;
l1 = l1.next;
}
l3 = l3.next;
}
l3.next = l1 || l2;
return head3.next;
};
mergeTwoLists([1,2,4],[1,3,4]);


I noticed this javascript code produces "Uncaught ReferenceError: ListNode is not defined at mergeTwoLists " which is coming from the third line when I try running it on my text editor but run's well on leetcode javascript editor
1. Please I want to know why the code is working on leetcode text editor and not working on the usual text editor.
2. Please I also need explanation on how l1.val, l2.val,l3.next,l2.next and l1.next properties are not returning un define. Because to the best of my understanding regular array does not have those value

Regarding your second question, I don’t think the input is an array. It should be a linked list containing those values. Even though it was written as an array, it just a way to give you an example of an input and the expected output. Even from the name “mergeList”, you should know the function expects a linked list.
Re: Leetcode Help by efelico: 3:13am On Aug 24, 2022
chukwuebuka65:


Regarding your second question, I don’t think the input is an array. It should be a linked list containing those values. Even though it was written as an array, it just a way to give you an example of an input and the expected output. Even from the name “mergeList”, you should know the function expects a linked list.
Thank you

(1) (Reply)

Upwork's Newest Scam Alert - Don't Fall For It! / Advice On Choice Laptop Please / Blogger

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 19
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.