class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
head = ListNode(-1)
p = head
carry = 0
while l1 or l2 or carry:
n = (l1.val if l1 else 0) + (l2.val if l2 else 0) + carry
carry = n // 10 # 取十位上的数
p.next = ListNode(n % 10)
p = p.next
l1, l2 = l1.next if l1 else None, l2.next if l2 else None
return head.next
【LC】面试题 02.05. 链表求和
最新推荐文章于 2022-05-20 23:00:06 发布