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!
You just keep another dummy pointer to the beginning of the list, which I have called newList. The other pointer like tmp or current keeps on moving to the next and next while constructing the list, but since you have another pointer at the beginning of list, you just return newList.next;. Hope that helps.
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?
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;
THANK YOU!!!!!!!!!!!!!!!!!!!!!!!
Glad it helped!
man you are o good at explaining this , please upload more .
You explain it so well ! Please do more leetcode explanations
Thank you for your kind words Abhishek! Will definitely try more. Have been busy with work so haven't gotten chance.
You do a good job explaining how to solve a problem.
Thanks bro
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!
You just keep another dummy pointer to the beginning of the list, which I have called newList. The other pointer like tmp or current keeps on moving to the next and next while constructing the list, but since you have another pointer at the beginning of list, you just return newList.next;. Hope that helps.
for the carry, totalsum / 10 gives a non integer, should you not use int(totalsum/10)?
Yes, it should have been carry = totalSum // 10 , since // does integer division in python
clear explanation . subscribed:)
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?
please do
Thank you!
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;
thank you bro!!
Thanks subbed
nice try