代码思路:如果值大于10,那么就进位到下一个位置l1.next,然后再让l1+l2更新l2的值
python
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode],carry=0) -> Optional[ListNode]:
if not l1:
return l2
if not l2:
return l1
l1.val += l2.val
if l1.val >= 10:
l1.next = self.addTwoNumbers(ListNode(l1.val // 10), l1.next)
l1.val %= 10
l1.next = self.addTwoNumbers(l1.next, l2.next)
return l1