我试图在排序的链表中插入一个节点,在插入后遍历它时,它显示出属性错误。它正在插入节点并遍历,但最后抛出错误。有人能解释一下这里出了什么问题吗?
def traversal(head):
curNode = head
while curNode is not None:
print(curNode.data , end = '->')
curNode = curNode.next
def insertNode(head,value):
predNode = None
curNode = head
while curNode is not None and curNode.data < value:
predNode = curNode
curNode = curNode.next
newNode = ListNode(value)
newNode.next = curNode
if curNode == head:
head = newNode
else:
predNode.next = newNode
return head
'builtin_function_or_method' object has no attribute 'x'
通常意味着您忘记将()
添加到函数调用中,例如
>>> 'thing'.upper.replace("H", "") #WRONG
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute 'replace'
>>> "thing".upper().replace("H", "") #RIGHT
'TING'
尽管您的代码看起来是对的,但我认为无论调用traversal
和insertNode
的是什么,都试图将函数的结果作为参数传递,但它却传递了函数(它缺少()
(