# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next classSolution: defaddTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: cur = dummy = ListNode() flag = 0 while l1 or l2 or flag: flag += (l1.val if l1 else0)+(l2.val if l2 else0) cur.next = ListNode(flag%10) flag //= 10 cur =cur.next if l1: l1=l1.next if l2: l2=l2.next