2. Add Two Numbers
l1
2
→4
→3
l2
5
→6
→4
carry: 0
2 + 5 + 0 = ?
answer
∅
digit 0: carry in = 0loop
1class Solution:2 def addTwoNumbers(self, l1, l2):3 head = current = None4 carry = 05 while l1 or l2 or carry:6 val1 = l1.val if l1 else 07 val2 = l2.val if l2 else 08 total = val1 + val2 + carry9 carry = total // 1010 digit = total % 1011 node = ListNode(digit)12 if head is None:13 head = current = node14 else:15 current.next = node16 current = current.next17 l1 = l1.next if l1 else None18 l2 = l2.next if l2 else None19 return head