2. 两数相加(Medium)
Python示例
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
dummpyHead = ListNode(None)
p = dummpyHead
digit = 0
while l1 or l2: # 两个链表都走完了
s = 0
if l1:
s += l1.val
l1 = l1.next
if l2:
s += l2.val
l2 = l2.next
s += digit
p.next = ListNode(s % 10)
p = p.next
digit = s // 10
if digit:
p.next = ListNode(digit)
return dummpyHead.next