class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(0)
curr = dummy
total = carry = 0
while l1 or l2 or carry:
total = carry
if l1:
total += l1.val
l1 = l1.next
if l2:
total += l2.val
l2 = l2.next
carry, val = divmod(total, 10)
temp = ListNode(val)
dummy.next = temp
dummy = dummy.next
return curr.next
"""
- The algorithms works just like our basic addition where we start from the right to left and
carry over any additional values to the next place.
- We only stop the iteration if BOTH lists pointing to null and we dont have any carry
- To find the carry, and the current node value we can use manual method (/10 and %10) or we can use
divmod(total, 10).
- Since the dummy node is initialized with 0, do dummy.next to attach the current value's node.
- Also dont forget to move the dummy pointer itself to next
- We cannot return dummy since its already at the end of the linkedlist, instead we will return the
curr because it will refer to the dummy list but the pointer is at the beginning.
- Return curr.next, if you return curr, the list will also include the initial 0, we want to exclude
the first zero.
"""