Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,732 members, 7,817,015 topics. Date: Friday, 03 May 2024 at 11:03 PM

Leetcode Solution Error Please Help Its Just Driving Me Crazy - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Leetcode Solution Error Please Help Its Just Driving Me Crazy (3476 Views)

Android Studio Error ..... Please I Need Help, I Don't Want To Suffer Again / Leetcode Help / Please Help, Its Urgent (2) (3) (4)

(1) (2) (Reply) (Go Down)

Leetcode Solution Error Please Help Its Just Driving Me Crazy by gistray: 2:17pm On Jan 15, 2022
So i am solving this problem on leetcode and my solutions runs as expected on the console.
But if I run the code on Leetcode is throws an error

THE QUESTION

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.
Example 2:

Input: l1 = [0], l2 = [0]
Output: [0]
Example 3:

Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]



MY SOLUTION

var addTwoNumbers = function(l1, l2) {

var alpha1 = ""
var alpha2 = ""
var arr = []


// Looping through first list l1

for(var i = 0; i< l1.length; i++){
alpha1 = l1[i]+alpha1

// converting l1 to string and setting it to ALPHA 1
}

// Looping through second list L2
for(var i = 0; i< l2.length; i++){
alpha2 = l2[i]+alpha2

// converting l2 to string and setting it to ALPHA 2
}
// Coverting alpha1 and apha2 to intergets and making their sum to a new variable string "New"
var New = parseInt(alpha1) + parseInt(alpha2)
New = New + ""

for(var i=0;i<New.length; i++){
// Pushing the New variable is reverse order to arr and also converting it back to interger
arr.push(parseInt(New[New.length-i-1]))

}
// Returning the array
return arr
};
addTwoNumbers([9,9,9,9,9,9,9],[9,9,9,9])



THE ERROR ON LEETCODE

Line 51 in solution.js
throw new TypeError(__serialize__(ret) + " is not valid value for the expected return type ListNode"wink;
^
TypeError: [null,null,null] is not valid value for the expected return type ListNode



WORST PART IS, THERE'S NO LINE 51 ON THE LEETCODE CODE MODULE LOL


I AM USING JAVASCRIPT
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by gistray: 2:31pm On Jan 15, 2022
HELLO
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by tensazangetsu20(m): 3:31pm On Jan 15, 2022
You aren't even meant to solve this question like this at all. This is the famous sum of two linked list question asked in companies like Facebook and Google. I will send my solution but in java later. Lemme book space.

2 Likes

Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by LikeAking: 3:55pm On Jan 15, 2022
U have learnt JS.

Nice one.

Use map.

use reduce.

reverse the arr with reverse.




Also drop the link.
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by gistray: 4:19pm On Jan 15, 2022
tensazangetsu20:
You aren't even meant to solve this question like this at all. This is the famous sum of two linked list question asked in companies like Facebook and Google. I will send my solution but in java later. Lemme book space.

I believe they are different ways to solve the question.

1 Like

Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by gistray: 4:19pm On Jan 15, 2022
LikeAking:
U have learnt JS.

Nice one.

Use map.

use reduce.

reverse the arr with reverse.




Also drop the link.

I was trying to avoid normal javascript array methods at all cost




Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by tensazangetsu20(m): 4:26pm On Jan 15, 2022
gistray:


I believe they are different ways to solve the question.
True but you aren't event iterating the linked list the right way. You are taking it as an array

2 Likes

Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by gistray: 4:29pm On Jan 15, 2022
tensazangetsu20:

True but you aren't event iterating the linked list the right way. You are taking it as an array

is there a list in javascript?
shocked shocked shocked that's whats confusing me
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by tensazangetsu20(m): 4:30pm On Jan 15, 2022
gistray:


is there a list in javascript?
shocked shocked shocked that's whats confusing me

There's none. I believe the question has created that data structure somewhere in the code for you to use. I use mostly java anyway when doing my algorithms but it's the same anyways.
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by LikeAking: 5:15pm On Jan 15, 2022
My solution. JS

function addTwoNumbers(a,b){

let xy = a.toString().replace(/,|\s/g,""wink

let xz = b.toString().replace(/,|\s/g,""wink

let zzz = Number(xy) + Number(xz)

let output = zzz.toString().split(""wink.reverse().join(""wink

return Number(output);

}
console.log(addTwoNumbers([9,9,9,9,9,9,9], [9,9,9,9]))

//output = 89990001

NL dey convert some brackets to this green guy wink.

Any wia u see am use ) = bracket
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by gistray: 6:01pm On Jan 15, 2022
LikeAking:
My solution.

function addTwoNumbers(a,b){

let xy = a.toString().replace(/,|\s/g,""wink

let xz = b.toString().replace(/,|\s/g,""wink

let zzz = Number(xy) + Number(xz)

return zzz.toString().split(""wink.reverse().join(""wink

}
console.log(addTwoNumbers([9,9,9,9,9,9,9], [9,9,9,9]))

output = 89990001

NL dey convert some brackets to this green guy wink.

Any wia u see am use ) = bracket

Hard to read.


How about not using regex
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by LikeAking: 7:00pm On Jan 15, 2022
gistray:


Hard to read.


How about not using regex

NL is formating the code.

Replace the green emoji with closing brackets.

The code is very simple.

Just paste in into ur editor.

drop d link to the problem.

Regex is the easiest.


Any wia u see "wink" in ur editor, replace with a closing bracket. ie )
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by Nobody: 7:14pm On Jan 15, 2022
tensazangetsu20:
You aren't even meant to solve this question like this at all. This is the famous sum of two linked list question asked in companies like Facebook and Google. I will send my solution but in java later. Lemme book space.
Yoo boss, you do Java now? kiss
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by tensazangetsu20(m): 7:17pm On Jan 15, 2022
Rgade:

Yoo boss, you do Java now? kiss


I only use java for algorithms o and salesforce.

1 Like

Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by tensazangetsu20(m): 7:17pm On Jan 15, 2022
public static class LinkedList {
public int value;
public LinkedList next;

public LinkedList(int value) {
this.value = value;
this.next = null;
}
}

public LinkedList sumOfLinkedLists(LinkedList linkedListOne, LinkedList linkedListTwo) {
LinkedList nextHeadPointer = new LinkedList(0);
LinkedList currentNode = nextHeadPointer;
int carry = 0;

LinkedList nodeOne = linkedListOne;
LinkedList nodeTwo = linkedListTwo;
while (nodeOne != null || nodeTwo != null || carry != 0){
int valueOne = (nodeOne != null) ? nodeOne.value : 0;
int valueTwo = (nodeTwo != null) ? nodeTwo.value : 0;
int sumOfValues = valueOne + valueTwo + carry;

int newValue = sumOfValues % 10;
LinkedList newNode = new LinkedList(newValue);
currentNode.next = newNode;
currentNode = newNode;

carry = sumOfValues / 10;
nodeOne = (nodeOne != null) ? nodeOne.next : null;
nodeTwo = (nodeTwo != null) ? nodeTwo.next : null;

}
return nextHeadPointer.next;
}

This is the solution in Java.
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by gistray: 7:46pm On Jan 15, 2022
tensazangetsu20:
public static class LinkedList {
public int value;
public LinkedList next;

public LinkedList(int value) {
this.value = value;
this.next = null;
}
}

public LinkedList sumOfLinkedLists(LinkedList linkedListOne, LinkedList linkedListTwo) {
LinkedList nextHeadPointer = new LinkedList(0);
LinkedList currentNode = nextHeadPointer;
int carry = 0;

LinkedList nodeOne = linkedListOne;
LinkedList nodeTwo = linkedListTwo;
while (nodeOne != null || nodeTwo != null || carry != 0){
int valueOne = (nodeOne != null) ? nodeOne.value : 0;
int valueTwo = (nodeTwo != null) ? nodeTwo.value : 0;
int sumOfValues = valueOne + valueTwo + carry;

int newValue = sumOfValues % 10;
LinkedList newNode = new LinkedList(newValue);
currentNode.next = newNode;
currentNode = newNode;

carry = sumOfValues / 10;
nodeOne = (nodeOne != null) ? nodeOne.next : null;
nodeTwo = (nodeTwo != null) ? nodeTwo.next : null;

}
return nextHeadPointer.next;
}

This is the solution in Java.


Mind adding some comments

1 Like

Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by gistray: 7:47pm On Jan 15, 2022
tensazangetsu20:

True but you aren't event iterating the linked list the right way. You are taking it as an array


Maybe I just convert the array to a list afterwards?
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by Deicide: 7:49pm On Jan 15, 2022
What you did there is not linked list
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by tensazangetsu20(m): 7:51pm On Jan 15, 2022
gistray:



Maybe I just convert the array to a list afterwards?

So practically you are going to construct the new LinkedList and as you are iterating both linked list you are going to add both values and put them in the new linked list then return back that list.
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by gistray: 7:56pm On Jan 15, 2022
tensazangetsu20:


So practically you are going to construct the new LinkedList and as you are iterating both linked list you are going to add both values and put them in the new linked list then return back that list.

No, convert the return array to a list in JavaScript.

What's important is the end product right?

I can't even tell what a list is in JavaScript... Is it an object or what?
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by tensazangetsu20(m): 7:58pm On Jan 15, 2022
gistray:


No, convert the return array to a list in JavaScript.

What's important is the end product right?

I can't even tell what a list is in JavaScript... Is it an object or what?



A linked list is its own data structure. You are taking it as an array but it isn't. That's why you are struggling.

2 Likes

Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by gistray: 7:59pm On Jan 15, 2022
Deicide:
What you did there is not linked list


What matters is the returned value right v
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by gistray: 7:59pm On Jan 15, 2022
tensazangetsu20:


A linked list is its own data structure. You are taking it as an array but it isn't. That's why you are struggling.


Can you help me in JavaScript?
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by tensazangetsu20(m): 8:01pm On Jan 15, 2022
gistray:


Can you help me in JavaScript?

Its the same thing I did too in Javascript the only difference is the LinkedList data structure will be a constructor( I dont think you have to created this, its always given in the code) and you add a math.floor to the new carry value in the while loop every other variable can be declared with const the ones reassigned can be done with let but its the exact same code.
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by gistray: 8:03pm On Jan 15, 2022
tensazangetsu20:


Its the same thing I did too in Javascript the only difference is the LinkedList data structure will be a constructor( I dont think you have to created this, its always given in the code) and you add a math.floor to the new carry value in the while loop every other variable can be declared with const the ones reassigned can be done with let but its the exact same code.

Ok sir
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by Deicide: 9:11pm On Jan 15, 2022
gistray:



What matters is the returned value right v
Leetcode linked list question uses classes so you would be doing something like x->next or x->value to access pointers and value, depending on the language you are using. And most of the time you are expected to return a node pointer. And I think [] is a stack in JavaScript not a list JavaScript doesn't have list.
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by Deicide: 9:15pm On Jan 15, 2022
gistray:


No, convert the return array to a list in JavaScript.

What's important is the end product right?

I can't even tell what a list is in JavaScript... Is it an object or what?


The confusion is with Leetcode it is not using JavaScript inbuilt container but Leetcode created a Linked list class that you would use.
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by Nobody: 9:35pm On Jan 15, 2022
tensazangetsu20:
public static class LinkedList {
public int value;
public LinkedList next;

public LinkedList(int value) {
this.value = value;
this.next = null;
}
}

public LinkedList sumOfLinkedLists(LinkedList linkedListOne, LinkedList linkedListTwo) {
LinkedList nextHeadPointer = new LinkedList(0);
LinkedList currentNode = nextHeadPointer;
int carry = 0;

LinkedList nodeOne = linkedListOne;
LinkedList nodeTwo = linkedListTwo;
while (nodeOne != null || nodeTwo != null || carry != 0){
int valueOne = (nodeOne != null) ? nodeOne.value : 0;
int valueTwo = (nodeTwo != null) ? nodeTwo.value : 0;
int sumOfValues = valueOne + valueTwo + carry;

int newValue = sumOfValues % 10;
LinkedList newNode = new LinkedList(newValue);
currentNode.next = newNode;
currentNode = newNode;

carry = sumOfValues / 10;
nodeOne = (nodeOne != null) ? nodeOne.next : null;
nodeTwo = (nodeTwo != null) ? nodeTwo.next : null;

}
return nextHeadPointer.next;
}

This is the solution in Java.
Omo shocked
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by LikeAking: 9:47pm On Jan 15, 2022
gistray:
So i am solving this problem on leetcode and my solutions runs as expected on the console.
But if I run the code on Leetcode is throws an error

THE QUESTION

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.
Example 2:

Input: l1 = [0], l2 = [0]
Output: [0]
Example 3:

Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]



MY SOLUTION

var addTwoNumbers = function(l1, l2) {

var alpha1 = ""
var alpha2 = ""
var arr = []


// Looping through first list l1

for(var i = 0; i< l1.length; i++){
alpha1 = l1[i]+alpha1

// converting l1 to string and setting it to ALPHA 1
}

// Looping through second list L2
for(var i = 0; i< l2.length; i++){
alpha2 = l2[i]+alpha2

// converting l2 to string and setting it to ALPHA 2
}
// Coverting alpha1 and apha2 to intergets and making their sum to a new variable string "New"
var New = parseInt(alpha1) + parseInt(alpha2)
New = New + ""

for(var i=0;i<New.length; i++){
// Pushing the New variable is reverse order to arr and also converting it back to interger
arr.push(parseInt(New[New.length-i-1]))

}
// Returning the array
return arr
};
addTwoNumbers([9,9,9,9,9,9,9],[9,9,9,9])



THE ERROR ON LEETCODE

Line 51 in solution.js
throw new TypeError(__serialize__(ret) + " is not valid value for the expected return type ListNode"wink;
^
TypeError: [null,null,null] is not valid value for the expected return type ListNode



WORST PART IS, THERE'S NO LINE 51 ON THE LEETCODE CODE MODULE LOL


I AM USING JAVASCRIPT

drop d link to d question.
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by islamics(m): 8:46pm On Jan 17, 2022
LikeAking:


drop d link to d question.

I think it is the second question in the list of problem in leetcode. Check the second question under problems.

gistray:


Can you help me in JavaScript?
Try checking out Alvin YouTube video tutorial on Data structure. He does his solutions using JavaScript.

Rgade:

Omo shocked
Real Omo.

1 Like

Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by LikeAking: 11:08pm On Jan 17, 2022
islamics:

I think it is the second question in the list of problem in leetcode. Check the second question under problems.


Try checking out Alvin YouTube video tutorial on Data structure. He does his solutions using JavaScript.


Real Omo.
ok!
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by Nonny12121: 9:39am On Feb 03, 2022
.

(1) (2) (Reply)

Beginning Microsoft C#.net / Creating Scratch Card Application / How Do I Pay For Apps On Google Play Store

(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. 56
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.