结题思路就不上传了,都是网上抄的,但是在 leetcode 上能通过 在本地就运行不了是什么原因,传入的到底是什么值阿? l1 = [2,3,4] l2 = [5,6,4] res = Solution().addTwoNumbers(l1, l2) print(res)
class ListNode: def init(self, x): self.val = x self.next = None
class Solution(object): def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ lret = ListNode(0) carryNum = 0 iterNode = lret iterL1 = l1 iterL2 = l2 while True: number = (iterL1.val if iterL1 else 0) + (iterL2.val if iterL2 else 0) + carryNum stopNum = number % 10 carryNum = number / 10 iterNode.val = stopNum iterL1 = iterL1.next if iterL1 else None iterL2 = iterL2.next if iterL2 else None if not iterL1 and not iterL2 and carryNum == 0: break else: nextNode = ListNode(0) iterNode.next = nextNode iterNode = nextNode return lret