# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: ans = ListNode(0) dummy = ans carry = 0 while l1 or l2 or carry: if l1 and l2: he = l1.val + l2.val + carry l1 = l1.next l2 = l2.next elif l1 and not l2: he = l1.val + carry l1 = l1.next elif l2 and not l1: he = l2.val + carry l2 = l2.next else: he = carry node_val = he % 10 carry = he // 10 dummy.next = ListNode(node_val) dummy = dummy.next return ans.next