Solution
- Need to keep
pre
and check ifpre
is 0 at the end. Cleaner Solutiondef addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ res = ListNode(0) tail = res pre = 0 while l1 or l2: val = pre if l1: val += l1.val l1 = l1.next if l2: val += l2.val l2 = l2.next tail.next = ListNode(val % 10) pre = val // 10 tail = tail.next while pre != 0: tail.next = ListNode(pre % 10) pre = pre // 10 tail = tail.next return res.next
PREVIOUSWord Search
NEXTLRU Cache