Leetcode加twosum的ListNode'未定义错误

时间:2019-02-20 20:04:41

标签: python

class Solution:
# @return a ListNode
    def addTwoNumbers(self, l1, l2):
        dummy = cur = ListNode(0)
        carry = 0
        while l1 or l2 or carry:
            if l1:
                carry += l1.val
                l1 = l1.next
            if l2:
                carry += l2.val
                l2 = l2.next
            cur.next = ListNode(carry%10)
            cur = cur.next
            carry //= 10
        return dummy.next

l1 = [203] # Array of numbers
l2 = [433]
s = Solution()
print(s.addTwoNumbers(l1, l2)) 

2#@返回一个ListNode       3 def addTwoNumbers(self,l1,l2): ----> 4个哑元= cur = ListNode(0)       5进位= 0       l1或l2或随身携带6:

NameError:未定义名称'ListNode'

2 个答案:

答案 0 :(得分:0)

ListNode应该由LeetCode定义,游乐场按钮中有调试代码,其中ListNode由LeetCode定义

答案 1 :(得分:0)

它最初是在编辑器面板中提供的,如下所示在Python2中:

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None