1. # Definition for singly-linked list.
    2. # class ListNode:
    3. # def __init__(self, x):
    4. # self.val = x
    5. # self.next = None
    6. class Solution:
    7. def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
    8. ans = ListNode(0)
    9. dummy = ans
    10. carry = 0
    11. while l1 or l2 or carry:
    12. if l1 and l2:
    13. he = l1.val + l2.val + carry
    14. l1 = l1.next
    15. l2 = l2.next
    16. elif l1 and not l2:
    17. he = l1.val + carry
    18. l1 = l1.next
    19. elif l2 and not l1:
    20. he = l2.val + carry
    21. l2 = l2.next
    22. else:
    23. he = carry
    24. node_val = he % 10
    25. carry = he // 10
    26. dummy.next = ListNode(node_val)
    27. dummy = dummy.next
    28. return ans.next