LeetCode Solution - 2.0 Add Two Numbers | Google Interview

preview_player
Показать описание
This is a detail solution to question number 2 in LeetCode called Add Two Numbers. This question uses LinkLists. I think this is a great question, although i think it probably should be an easy instead of medium.

Please feel free to drop any question or comments
Рекомендации по теме
Комментарии
Автор

Hey Man, I just want to appreciate your work! Thank you for all your efforts, this series is really a gem for me! love from India

roshankumarsharma
Автор

you are great man!....you explain things in a way that it becomes really easy to solve this stuff

vivarantx
Автор

Man! I cannot thank you enough for sharing your knowledge! It seemed as though you knew the syntax in each line before you wrote the code!

What needs to be done to create the syntax of a program with that same level of confidence in knowing what to write next?

GraceandWisdom
Автор

Great video man, keep them coming. Helps a lot

ucheuba
Автор

Cannot understand how "return solution.next" is returning an array. Isn't it an object with value new Listnode(); Stuck in this part.

sdas
Автор

sorry for the dumb question. I am new to leetcode and coding. what does ListNode and next stand for in this case? that's not a JS method is it? little confused. sorry I'm new.

orlandovaladez
Автор

Seems a little complicated unless I am misunderstanding.

Why can't we just pull the numbers out into a string, convert to int, add the numbers, and then push each individual digit into a new linked list and return? And you could just push it back in reverse order as well.


Edit: A slow, and stupid solution, but it passes 😂

/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/

var addTwoNumbers = function(l1, l2) {
let str1 = '';
let str2 = '';
let tempstr = '';

while (l1 || l2){

if(l1){
str1 = str1 + l1.val
l1 = l1.next;
}
if(l2){
str2 = str2 + l2.val
l2 = l2.next;
}
}


tempstr =
+


tempstr = tempstr.toLocaleString('fullwide', {useGrouping:false})

let ListHead = new ListNode(0);
let p2h = ListHead;

for (var i = tempstr.length - 1; i >= 0; i--) {
ListHead.val = parseInt(tempstr.charAt(i));
if(i !== 0){
ListHead.next = new ListNode(0);
ListHead = ListHead.next
}
}

return p2h;

};

discontinuity
Автор

im practising js for interviews.

I am away from js for quite awhile, can anyone explain to me how is `solution` being set?

I understand the solution, but the way how it works is confusing me ...

enkr
Автор

Why'd you stop? You're such a good teacher T.T

gihdarlewa
Автор

Hello,
But i think we don't need pointers in linked List, could you please explain why we have used pointers also?

ishumishra
Автор

man this guy telling us this is easy, i couldn't figure it out lol

jiayang
Автор

Please feel free to drop any comments or questions! you can be the first ! I will reply to all message! Thanks! LeetCode Solution here. According to LeetCode, this is a google question?

ThinkFWD
Автор

I'm definitely don't understand 😁

sokpheachea
Автор

how about this
var addTwoNumbers = function(l1, l2) {
let a = l1.reverse().toString()
let b = l2.reverse().toString()
const sum = parseInt(a.split(", ").join(""), 10) + parseInt(b.split(", ").join(""), 10)
return Array.from(String(sum), num => Number(num))
};

altayezekariyas