Python 有人能解释self.nextNode.remove(数据,self)如何调用remove()方法吗?
我猜这是链表中的一个节点Python 有人能解释self.nextNode.remove(数据,self)如何调用remove()方法吗?,python,data-structures,Python,Data Structures,我猜这是链表中的一个节点 class node: def __init__(self,data): self.data=data self.nextNode=None def remove(self,data,previousNode): if self.data == data: previousNode.nextNode=self.nextNode del self.data
class node:
def __init__(self,data):
self.data=data
self.nextNode=None
def remove(self,data,previousNode):
if self.data == data:
previousNode.nextNode=self.nextNode
del self.data
del self.nextNode
else:
if self.nextNode is not None:
self.nextNode.remove(data,self)
您不必
删除self.data
和删除self.nextNode
。。。但话说回来,你到底在问什么?你不明白的是什么?请详细说明问题中的标题。
def remove(self,data,previousNode):
if self.data == data: # we hit the node we want to delete
previousNode.nextNode=self.nextNode # we disconnect the current node from the link, connect the next node to its previous node
del self.data # not exactly sure about these two lines
del self.nextNode
else: # we haven't hit the node we want to delete
if self.nextNode is not None: # the list hasn't end, there is a "next"
self.nextNode.remove(data, self)
# this is the tricky part,
# you need to consider this in nextNode's perspective.
# You're calling nextNode's remove method,
# telling it the data you need to remove, and tell it
# that it's previousNode is your current node (self)