Python 计算链表中的值数

Python 计算链表中的值数,python,python-2.7,Python,Python 2.7,我的countInt函数有问题。除此之外,它被标记为countINT,并且我将'-'作为一个参数放入其中。我测试了我的链表的创建,它似乎工作正常。所以我觉得我可以安全地排除这个问题。然而,由于非类型对象没有属性值错误,我不确定哪里出错了。有没有人能成为我的另一双眼睛,帮我找出错误并指导我改正?非常感谢 输出: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "l

我的
countInt
函数有问题。除此之外,它被标记为countINT,并且我将
'-'
作为一个参数放入其中。我测试了我的链表的创建,它似乎工作正常。所以我觉得我可以安全地排除这个问题。然而,由于非类型对象没有属性值错误,我不确定哪里出错了。有没有人能成为我的另一双眼睛,帮我找出错误并指导我改正?非常感谢

输出:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "linked_list.py", line 63, in <module>
    print countInt(r,1)
  File "linked_list.py", line 25, in countInt
    countInt(head.next,n)
  File "linked_list.py", line 25, in countInt
    countInt(head.next,n)
  File "linked_list.py", line 25, in countInt
    countInt(head.next,n)
  File "linked_list.py", line 25, in countInt
    countInt(head.next,n)
  File "linked_list.py", line 25, in countInt
    countInt(head.next,n)
  File "linked_list.py", line 25, in countInt
    countInt(head.next,n)
  File "linked_list.py", line 25, in countInt
    countInt(head.next,n)
  File "linked_list.py", line 25, in countInt
    countInt(head.next,n)
  File "linked_list.py", line 25, in countInt
    countInt(head.next,n)
  File "linked_list.py", line 25, in countInt
    countInt(head.next,n)
  File "linked_list.py", line 25, in countInt
    countInt(head.next,n)
  File "linked_list.py", line 25, in countInt
    countInt(head.next,n)
  File "linked_list.py", line 23, in countInt
    if head.next.value == n:
AttributeError: 'NoneType' object has no attribute 'value'
我的代码:

class Node:
    def __init__(self,value):
        self.next = None
        self.value = value

def createLinkedList(root, node):
    if root.next is None:
        root.next = node
    else:
        createLinkedList(root.next, node)

def countInt(head, n, count= 0):        #create a dictionary with keys as the values of the linked list and count up if the value occurs again
    count = 0
    if head.value is None:
        return None
    else:
        if head.next.value == n:
            count += 1
        countInt(head.next, n, count)
        return count


# root
r = Node(1)

# nodes
a = Node(4)
b = Node(1)
c = Node(5)
d = Node('-')
e = Node(4)
f = Node(1)
g = Node(2)
h = Node('-')
i = Node(8)
j = Node(9)
k = Node(8)
l = Node(3)

createLinkedList(r,a)
createLinkedList(r,b)
createLinkedList(r,c)
createLinkedList(r,d)
createLinkedList(r,e)
createLinkedList(r,f)
createLinkedList(r,g)
createLinkedList(r,h)
createLinkedList(r,i)
createLinkedList(r,j)
createLinkedList(r,k)
createLinkedList(r,l)


print countInt(r,1)
print countInt(r,'-')
更改行:

if head.value is None:

该行的目的是知道列表何时应该停止,下一个节点将在哪一点
None
。存储在节点中的值与此无关:最后一个节点仍将有一个值(相反,您可能希望在列表的前面存储值
None


作为一个单独的问题,您的函数将始终返回0。行
countInt(head.next,n,count)
实际上对变量
count
没有任何作用:Python按值传递int,因此传递的变量不会递增。即使你解决了这个问题,你总是检查
head.next
,这意味着你跳过了列表中的第一个元素。您应该将您的功能设置为:

def countInt(head, n):
    count = 0
    if head.value == n:
        count = 1
    if head.next is None:
        return count
    return count + countInt(head.next, n)

这实际上可以让您摆脱通过列表向下传递
count
(这是实现递归的一种笨拙方式)。

但是,如果我只输入一个值为n的节点,该怎么办?@Liondancer:要点:我建议的代码顺序错误,在这种情况下无法工作。固定的
if head.next is None:
def countInt(head, n):
    count = 0
    if head.value == n:
        count = 1
    if head.next is None:
        return count
    return count + countInt(head.next, n)