为什么 self.head = None 删除链表中的每个元素?

发布于 2025-01-17 18:27:09 字数 1448 浏览 2 评论 0原文

我是第一次学习链接列表,觉得我对在链接列表的开头或结束时插入插入方面有很好的了解。

但是,我看到,如果我有一个现有的链接列表,并且我做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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

腹黑女流氓 2025-01-24 18:27:09

如果您熟悉寻宝游戏类型游戏,则只有在解决线索时才能找到下一个线索的位置。在游戏开始时,您可以获得第一个线索的位置。如果我从来没有给你第一个线索怎么办?您可以玩游戏吗?

在链接列表中,每个节点都包含对下一个节点的引用。设置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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文