Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,897 members, 7,821,125 topics. Date: Wednesday, 08 May 2024 at 08:39 AM

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

Nairaland Forum / Science/Technology / Programming / Leetcode Solution Error Please Help Its Just Driving Me Crazy (3484 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)

Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by jikins(m): 6:10pm On Feb 03, 2022
gistray:
HELLO

This is pretty straightforward in JS. Shouldn't take you more than 10 lines of code if you use array and string methods.

Using the 1st example

First you reverse the arrays, then join them together to form a single string so [ 2, 4, 3 ] will become "342". After which you convert the string to number. Add both numbers together which should give you 807

Then convert the answer back to a string. Split it into an array so 807 becomes ["8", "0", "7"]. Reverse it then convert each member of the array from a string back to a number

Heres the code.

const solve = (arr1, arr2) => {
const no1 = Number(arr1.reverse().join("" ) )
const no2 = Number(arr2.reverse().join("" ) )

const ans = no1 + no2

return String(ans).split("" ).reverse().map(Number)
}
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by gistray: 6:15pm On Feb 03, 2022
jikins:


This is pretty straightforward in JS. Shouldn't take you more than 10 lines of code if you use array and string methods.

Using the 1st example

First you reverse the arrays, then join them together to form a single string so [ 2, 4, 3 ] will become "342". After which you convert the string to number. Add both numbers together which should give you 807

Then convert the answer back to a string. Split it into an array so 807 becomes ["8", "0", "7"]. Reverse it then convert each member of the array from a string back to a number

Heres the code.

const solve = (arr1, arr2) => {
const no1 = Number(arr1.reverse().join("" ) )
const no2 = Number(arr2.reverse().join("" ) )

const ans = no1 + no2

return String(ans).split("" ).reverse().map(Number)
}


You obviously don't understand the question... sorry

1 Like

Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by jikins(m): 6:16pm On Feb 03, 2022
gistray:



You obviously don't understand the question... sorry

How do you mean?
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by jikins(m): 7:04pm On Feb 03, 2022
gistray:



You obviously don't understand the question... sorry

Oh I see it now its a Linkedlist not an array. Have you been able to do it by any chance?
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by gistray: 7:28pm On Feb 03, 2022
jikins:


Oh I see it now its a Linkedlist not an array. Have you been able to do it by any chance?

My solution is working on the console
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by jikins(m): 8:37pm On Feb 03, 2022
gistray:


My solution is working on the console

Yeah, do you have a link to the question on leet code?
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by TheManOfTheYear: 9:28am On Feb 04, 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.

Exactly. But we don't use linked list in JS, how did you get your answer please
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by tensazangetsu20(m): 9:36am On Feb 04, 2022
TheManOfTheYear:
Exactly. But we don't use linked list in JS, how did you get your answer please

You have to create your own LinkedIn data structure using a constructor. The question already does this for you anyway.
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by TheManOfTheYear: 9:39am On Feb 04, 2022
tensazangetsu20:


You have to create your own LinkedIn data structure using a constructor. The question already does this for you anyway.
Thanks man, you too much
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by airsaylongcome: 10:14am On Feb 04, 2022
tensazangetsu20:


You have to create your own LinkedIn data structure using a constructor. The question already does this for you anyway.

I don't understand people coming here to day "Javascript doesn't have Linked List". Like seriously?! Which Programming language has Linked Lists as a native data structure? I learnt Data Structures and Algorithms in 2003 using Pascal as the programming language of choice at the time. And we learn Linked List, even double linked lists having two ptrs (forward and back) with the value/data "sandwiched" between. I see people here diving into this with arrays and that's a definite failure from the get go
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by jikins(m): 6:11pm On Feb 07, 2022
gistray:


My solution is working on the console

Check this

const makeNode = (value) => {
return {
value: value,
next: null
}
}

class List {
constructor() {
this.head = null;
this.tail = null;
}

add(value) {
let node = makeNode(value);
if (!this.tail) {
this.head = this.tail = node;
return node;
}
this.tail.next = node;
this.tail = node;
return node;
}

print() {
let current = this.head;
while (current) {
console.log(current.value)
current = current.next;
}
}

addEachArrayValuesAsNode(arr) {
arr.forEach(value => {
this.add(value)
})
}
}

const l1 = new List
const l2 = new List

l1.addEachArrayValuesAsNode([2, 4, 3])
l2.addEachArrayValuesAsNode([5, 6, 4])


const printListAsNumberInReverse = (list) => {
const arr = []
let current = list.head;
while(current) {
arr.push(current.value)
current = current.next;
}
return Number(arr.reverse().join(""wink )
}

const addLinkedListInReverse = (list1, list2) => {
const finalAnswerLinkedList = new List
const list1CorrectedValue = printListAsNumberInReverse(list1)
const list2CorrectedValue = printListAsNumberInReverse(list2)
const sumOfValues = list1CorrectedValue + list2CorrectedValue
const sumOfValuesInReverseArray = String(sumOfValues).split("" ).reverse().map(Number)
finalAnswerLinkedList.addEachArrayValuesAsNode(sumOfValuesInReverseArray)
return finalAnswerLinkedList
}

console.log(addLinkedListInReverse(l1, l2))

you can do console.log(addLinkedListInReverse(l1, l2).print() ) to get the values of each node listed on console
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by gistray: 6:49pm On Feb 07, 2022
jikins:


Check this

const makeNode = (value) => {
return {
value: value,
next: null
}
}

class List {
constructor() {
this.head = null;
this.tail = null;
}

add(value) {
let node = makeNode(value);
if (!this.tail) {
this.head = this.tail = node;
return node;
}
this.tail.next = node;
this.tail = node;
return node;
}

print() {
let current = this.head;
while (current) {
console.log(current.value)
current = current.next;
}
}

addEachArrayValuesAsNode(arr) {
arr.forEach(value => {
this.add(value)
})
}
}

const l1 = new List
const l2 = new List

l1.addEachArrayValuesAsNode([2, 4, 3])
l2.addEachArrayValuesAsNode([5, 6, 4])


const printListAsNumberInReverse = (list) => {
const arr = []
let current = list.head;
while(current) {
arr.push(current.value)
current = current.next;
}
return Number(arr.reverse().join(""wink )
}

const addLinkedListInReverse = (list1, list2) => {
const finalAnswerLinkedList = new List
const list1CorrectedValue = printListAsNumberInReverse(list1)
const list2CorrectedValue = printListAsNumberInReverse(list2)
const sumOfValues = list1CorrectedValue + list2CorrectedValue
const sumOfValuesInReverseArray = String(sumOfValues).split("" ).reverse().map(Number)
finalAnswerLinkedList.addEachArrayValuesAsNode(sumOfValuesInReverseArray)
return finalAnswerLinkedList
}

console.log(addLinkedListInReverse(l1, l2))

you can do console.log(addLinkedListInReverse(l1, l2).print() ) to get the values of each node listed on console

Copied?
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by jikins(m): 8:34pm On Feb 07, 2022
gistray:


Copied?

Nope, took a few hours looking at articles to get the general idea, then came up with this. It works! Didnt try it with the other examples though but it should do the job i feel. Hope you get it tho, I tried naming the function with what they do.
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by deept(m): 11:22am On Mar 08, 2022
airsaylongcome:


I don't understand people coming here to day "Javascript doesn't have Linked List". Like seriously?! Which Programming language has Linked Lists as a native data structure? I learnt Data Structures and Algorithms in 2003 using Pascal as the programming language of choice at the time. And we learn Linked List, even double linked lists having two ptrs (forward and back) with the value/data "sandwiched" between. I see people here diving into this with arrays and that's a definite failure from the get go

i saw this and you came to mind

https://www.linkedin.com/posts/javascript-developer_activity-6906851846259150849-jYmF
Re: Leetcode Solution Error Please Help Its Just Driving Me Crazy by Playermayweda(m): 6:47pm On Mar 11, 2022
for(var i=New.length-1; i<=0; i--){
// Pushing the New variable is reverse order to arr and also converting it back to interger
arr.push(parseInt(New[i]))

}
// Returning the array
return arr
};

Pls if this won't work , why??

(1) (2) (Reply)

Windows 8.1 Product Key / Who Wants To Learn Programming In Python Easily Without Stress? READ THIS / Online Banking Php Script

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