Leetcode #2 - Add Two Numbers (Solution)

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

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

You do a good job explaining how to solve a problem.

rayprusia
Автор

man you are o good at explaining this, please upload more .

tripathi
Автор

You explain it so well ! Please do more leetcode explanations

Abhishek
Автор

Thank you for the explanation. Can you explain why newlist.next gives all the digits of the sum (708) rather than only the first digit of the sum which is 7. How come "next" gives the whole list of values?

mahsaz
Автор

just getting started with relearning linked lists. How does the created list solution get connected back to ret.next

I can visualize how curr.next makes a new node and setting curr=curr.next shifts the pointer to the new node for the next iteration. Is it from a background function I don't see, or is it just how returns work and that's what I need to study. Thank you!

The-Entelechy
Автор

for the carry, totalsum / 10 gives a non integer, should you not use int(totalsum/10)?

shahzadkarim
Автор

does this work when the li = [5] and l2 = [5] ? I was trying the same in JS, and it fails for this test case.. here is my code






var tempList = new ListNode(0);
var head = tempList;
var sum = 0;
var carry = 0;

while (l1 || l2) {
if (l1) {
sum += l1.val; // 3
l1 = l1.next; // 4 node
}

if (l2) {
sum += l2.val; // 3+4=7
l2 = l2.next; // 6 node
}

sum = sum + carry; // 7+0=7
carry = Math.floor(sum/10); // 0

head.next = new ListNode(sum % 10); // 7
sum = 0;
head = head.next; // 7
}

return tempList.next;

prashanthnaik
join shbcf.ru