LeetCode-Linked 2020-01-13发表 2022-08-06更新 Algorithm / LeetCode 几秒读完 (大约89个字) 0次访问 2. Add Two Numbers 123456789101112131415161718192021# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: ret = ListNode(0) p = ret r = 0 while(r or l1 or l2): a1 = 0 if not l1 else l1.val l1 = None if not l1 else l1.next a2 = 0 if not l2 else l2.val l2 = None if not l2 else l2.next s = a1 + a2 + r r = s // 10 p.next = ListNode(s % 10) p = p.next return ret.nextLeetCode-Linkedhttp://blog.czccc.cc/p/1d0da4bd/作者Cheng发布于2020-01-13更新于2022-08-06许可协议