Python 链表属性错误:'NoneType'对象没有属性'next'



我正在尝试编写一个程序来从链表中删除重复值

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

class LinkedList(object):
def __init__(self):
self.head = None
def append(self, data):
node = self.head
if(node == None):
self.head = Node(data)
return
while(node.next):
node = node.next
node.next = Node(data)
return
def removeDups(ll):
if(ll.head is None):
return
headNode = ll.head
while(headNode):
lead = headNode
while(lead.next):
if(lead.next.data == headNode.data):
lead.next = lead.next.next
lead = lead.next
headnode = headNode.next
return
list = LinkedList()
list.append(1)
list.append(2)
list.append(3)
list.append(1)
removeDups(list)

错误指向"while(lead.next(:"处的行,Arribute 错误"NoneType"对象没有属性"next">

我不能为同一个列表节点分配两个值吗?是头节点和引导指针吗?

这应该适合您。您需要对 removeDups 函数进行两项更改:

def removeDups(ll):
if(ll.head is None):
return
headNode = ll.head
while(headNode):
lead = headNode
while(lead and lead.next):
if(lead.next.data == headNode.data):
lead.next = lead.next.next
lead = lead.next
headNode = headNode.next
return

相关内容

  • 没有找到相关文章

最新更新