https://leetcode.com/problems/add-two-numbers/discuss/447910/Python-Faster-than-93.43
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
p, q = l1, l2
cur = dummy = ListNode(-1)
carry = 0
while p and q:
temp = p.val + q.val + carry
cur.next = ListNode(temp % 10)
carry = 1 if temp >= 10 else 0
cur = cur.next
p = p.next
q = q.next
while p:
temp = p.val + carry
cur.next = ListNode(temp % 10)
carry = 1 if temp >= 10 else 0
p = p.next
cur = cur.next
while q:
temp = q.val + carry
cur.next = ListNode(temp % 10)
carry = 1 if temp >= 10 else 0
q = q.next
cur = cur.next
if carry == 1:
cur.next = ListNode(1)
return dummy.next
来源:CSDN
作者:olala_michelle
链接:https://blog.csdn.net/weixin_44582464/article/details/103473436