Add Two Numbers Leetcode problem

Hello! I’ve decided to continue practicing my algorithms with After completing the JavaScript curriculum, I feel like I still need extra practice (especially after doing some research about the technical interview process).

I’m working on this problem

I’m using codepen to work through the algorithm and came up with something that passes the test cases:

But upon submitting the exact same code into the leetcode editor, I get this message :

Screen Shot 2021-06-02 at 2.23.44 PM

It doesn’t seem to be taking an abnormally long time for codepen to render the solution in the console, so I’m confused as to why I would get a runtime error in leetcode.

Have you completed the algorithm challenges in FCC? They are pretty good. I’m not sure about this specific case. But I know that some of these tests limit the amount of memory your code is allowed to use and it appears that you’ve exceeded that amount. The error message references node which leads me to believe the test is running in a different runtime than where you initially wrote the solution. So the memory limit may be different.

Hi, I am looking at your codepen and you seem to have coded this challenge with two arrays as inputs. This LC uses a data structure called linked lists, which despite the way LC’s presentation as arrays, are very different from them.

If you have not learned about nodes&linked lists before, I suggest coming back to this after you’ve grasped the concept through online resources, one of which is FCC’s own algorithm challenge which I believe does covered linked lists.

If you have learned about linked lists, here is some pseudo code which may help you if you wish:

variable carry stores addition result of current node pair
initialize linked list head
variable curr points to head by reference

while (carry>0 or l1 exists or l2 exists) {
  carry = value of l1 node (if exists) + value of l2 node (if exists)
  attach new node to curr with value = carry%10
  move curr pointer to new node
  carry = floor(carry/10)
  if  l1 exists, move l1 pointer to
  if l2 exists, move l2 pointer to

Thanks! I am not familiar with linked lists and I don’t remember ever coming across linked lists in FCC’s algorithm challenges. I’ll come back to it after getting more familiar with them.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.