Open mengyushi opened 4 years ago
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: dummy = ListNode(None) pre = dummy carry = 0 while l1 and l2: val = l1.val + l2.val + carry if val >= 10: val-=10 carry = 1 else: carry = 0 pre.next = ListNode(val) pre = pre.next l1, l2 = l1.next, l2.next l = l1 or l2 while l: val = l.val + carry if val >= 10: val-=10 carry = 1 else: carry = 0 pre.next = ListNode(val) pre = pre.next l = l.next if carry: pre.next = ListNode(1) return dummy.next