LeetCode Add Two Numbers Solution Explained - Java

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

Great.. one suggestion though. Please move your picture position to down left.

ManishSingh-djyu
Автор

Some how this was the only video that could explain the solution to me. thanks

reggiecook
Автор

the numbers are bit confusing here because 243+564 = 807 and if you reverse the numbers 342+465 = 807

shyamutty
Автор

Hey Nick, Thanks for the explanation. It would be a great help if you provide the solution of problem 445 i.e Add two numbers II.

NavedKhan-jeel
Автор

i get almost all of it, i can see how its added and carried over, i just dont understand which part revereses the linked node so that it can be added the way it is added

tiffanywong
Автор

thanks for sharing. very clear explanation. Im preparing for my interview, I like your video

laurawu
Автор

I dont get it. When we never assigned any next Node to dummy_head. How did dummy_head still print the right answer?

leetcodebaby
Автор

Thank you! This was the first time I was able to understand the solution to this problem.

jennalefort
Автор

Really easy tutorial to follow, thanks a ton!

tadashimatsumoto
Автор

hey. thanks for the video. Would be great if you could either decrease the sound of the keyboard clicks or use a better microphone.

HiBMlive
Автор

Thank you brother for your explanation.

riyasvaliyadan
Автор

Imo it’s more convenient (val_1 + val_2) % 10 further we can check if val_1 + val2 >= 10

Munchen
Автор

ListNode dummy_head = new ListNode(0);

what does the (0) mean, why we have it there?

maherjabbar
Автор

is that capital one's hack t-shirt ur wearing?

ff
Автор

What if we have much larger numbers? Sum must be long type right

nitinkallappa
Автор

You could improve this further by doing it with O(1) space if you reuse nodes of the longer number

coolilshat
Автор

Do you practice all problem beforehand and them present them here or is it you solve on the spot ??

ziakhan-tkrk
Автор

Passes 1560/1563 test cases.


class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
StringBuilder s1 = new StringBuilder("");
StringBuilder s2 = new StringBuilder("");
while(l1!=null){
s1.append(l1.val);
l1 = l1.next;
}
while(l2!=null){
s2.append(l2.val);
l2 = l2.next;
}
String t1 = s1.reverse().toString();
String t2 = s2.reverse().toString();
long i1 = Long.parseLong(t1);
long i2 = Long.parseLong(t2);
long i3 = i1+i2;
if(i3==0){
return new ListNode(0);
}
ListNode dummy = new ListNode(-1);
ListNode head = dummy;
while(i3!=0){
int k = (int)(i3%10);
i3 = i3/10;
head.next = new ListNode(k);
head = head.next;
}
return dummy.next;


}
}

iamnoob
Автор

wat about the 0 in the list during initialization wouldnt that lead to a wrong answer?

tdizzleshizzle
Автор

how is the carry not being brought over from the first time? wouldn’t it be .7?

kimieti