为什么 self.head = None 删除链表中的每个元素?
我是第一次学习链接列表,觉得我对在链接列表的开头或结束时插入插入方面有很好的了解。
但是,我看到,如果我有一个现有的链接列表,并且我做self.head = none
,那么它将摆脱每个元素,从而为我提供一个空的链接列表。引擎盖下发生了什么?为什么会发生这种情况?任何信息将不胜感激。
class Node:
def __init__(self, data = None, next = None):
self.data = data
self.next = next
class LinkedList:
def __init__(self):
self.head = None
def insert_at_beginning(self, data):
node = Node(data, self.head)
self.head = node
def print(self):
if self.head is None:
print("linked list is empty")
return
itr = self.head
llstr = ''
while itr:
llstr += str(itr.data) + '-->'
itr = itr.next
print(llstr)
def insert_at_end(self, data):
if self.head is None:
self.head = Node(data, None)
return
itr = self.head
while itr.next:
itr = itr.next
itr.next = Node(data, None)
def clear_linked_list(self):
self.head = None
return
def insert_values(self, data_list):
self.clear_linked_list()
for data in data_list:
self.insert_at_end(data)
if __name__ == "__main__":
ll = LinkedList()
ll.insert_at_beginning(5)
ll.insert_at_beginning(89)
ll.insert_at_end(90)
ll.print()
ll.insert_values(['nothing', 'test'])
ll.print()
I am learning about linked lists for the first time and feel like I am getting a pretty good understanding of inserting at the beginning or end of a linked list.
However, I see that if I have an existing linked list and I do self.head = None
then it gets rid of every element, thus giving me an empty linked list. What is happening under the hood? Why does this happen? Any information is appreciated.
class Node:
def __init__(self, data = None, next = None):
self.data = data
self.next = next
class LinkedList:
def __init__(self):
self.head = None
def insert_at_beginning(self, data):
node = Node(data, self.head)
self.head = node
def print(self):
if self.head is None:
print("linked list is empty")
return
itr = self.head
llstr = ''
while itr:
llstr += str(itr.data) + '-->'
itr = itr.next
print(llstr)
def insert_at_end(self, data):
if self.head is None:
self.head = Node(data, None)
return
itr = self.head
while itr.next:
itr = itr.next
itr.next = Node(data, None)
def clear_linked_list(self):
self.head = None
return
def insert_values(self, data_list):
self.clear_linked_list()
for data in data_list:
self.insert_at_end(data)
if __name__ == "__main__":
ll = LinkedList()
ll.insert_at_beginning(5)
ll.insert_at_beginning(89)
ll.insert_at_end(90)
ll.print()
ll.insert_values(['nothing', 'test'])
ll.print()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您熟悉寻宝游戏类型游戏,则只有在解决线索时才能找到下一个线索的位置。在游戏开始时,您可以获得第一个线索的位置。如果我从来没有给你第一个线索怎么办?您可以玩游戏吗?
在链接列表中,每个节点都包含对下一个节点的引用。设置
self.head
等同于给您第一个线索。如果您摆脱了第一个节点,则无法知道哪个节点是第二个节点,因为该信息仅在您刚刚摆脱的第一个节点中。If you're familiar with scavenger hunt-type games, you find the location of the next clue only when you solve a clue. You get the location of the first clue at the start of the game. What if I never gave you the first clue? Would you be able to play the game?
In a linked list, each node contains a reference to the next node. Setting
self.head
is equivalent to giving you the first clue. If you get rid of the first node, there's no way to know which is the second node because that information was only in the first node which you just got rid of.