Add Two Numbers

Key Idea

Solution

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
        # Initialize
        dummy = ListNode()
        current = dummy
        carry = 0

        # Iterate if any contains value
        while l1 or l2 or carry:
            # Set val1 and val2
            val1 = l1.val if l1 else 0
            val2 = l2.val if l2 else 0
            # Get the total
            total = val1 + val2 + carry

            # Find digit and carry
            digit = total % 10
            carry = total // 10

            # Set the next node to digit
            current.next = ListNode(digit)
            # Move the "output pointer" to next
            current = current.next

            # Move the list pointers
            if l1:
                l1 = l1.next
            if l2:
                l2 = l2.next
        # Return
        return dummy.next

Complexity