Python 中的 LinkedList 实现未显示在头节点之外

LinkedList implementation in Python not showing beyond the head node

我在 Python 中尝试从头开始创建链表的尝试没有成功,而且我不确定我遗漏了什么。我尝试为节点和链表创建单独的 classes,但是当我试图查看头节点以外的内容时,我遇到了障碍。 感谢任何指针(没有双关语)。另外,什么是更好的方法:为节点设置单独的 class,或者使它们成为链表 class 本身的属性?

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class LinkedList:
    def __init__(self):
        self.head = None
    
    # adding/inserting to the tail
    def add(self, val):
        node_to_add = ListNode()
        if self.head == None:
            self.head = node_to_add
            node_to_add.val = val
        else:
            self.next = node_to_add
            node_to_add.val = val 

    # printing the linked list as a list
    def print(self):
        list_to_print = []
        if not self.head:
            return None
        node_to_read = self.head
        while self.head:
            list_to_print.append(node_to_read.val)
            if node_to_read.next:
                self.head = node_to_read.next
            else:
                return list_to_print

当我运行这段代码时,我只能打印头节点。当我在第一个条目后添加 nodes/values 时,print() 仅 returns head.

test1 = LinkedList()
test1.add(1)
test1.add(4)
test1.add(7)
test1.print()

输出是

[1]

为了将项目附加到最后一个节点,您需要跟踪最后一个节点。

您也不应该在打印函数中覆盖头指针。

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class LinkedList:
    def __init__(self):
        self.head = None
        self.tail = None

    # adding/inserting to the tail
    def add(self, val):
        node_to_add = ListNode()
        node_to_add.val = val
        if self.tail == None:
            self.head = node_to_add
            self.tail = node_to_add
        else:
            self.tail.next = node_to_add
            self.tail = node_to_add


            # printing the linked list as a list

    def print(self):
        list_to_print = []
        if not self.head:
            return None
        current_node = self.head
        while current_node:
            list_to_print.append(current_node.val)
            if current_node.next:
                current_node = current_node.next
            else:
                return list_to_print

您可以通过让节点负责添加子节点来显着简化这一过程。这使得链表对象基本上是一个包装器,用于管理列表的头部和迭代:

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
        
    def add(self, node):
        if self.next is None:
            self.next = node
        else:
            self.next.add(node)

class LinkedList:
    def __init__(self):
        self.head = None
    
    def add(self, val):
        node = ListNode(val)
        if self.head == None:
            self.head = node
        else:
            self.head.add(node)

    # printing the linked list as a list
    def print(self):
        list_to_print = []
        node_to_read = self.head
        
        while node_to_read:
            list_to_print.append(node_to_read.val)
            node_to_read = node_to_read.next
            
        print(list_to_print)
            
test1 = LinkedList()
test1.add(1)
test1.add(4)
test1.add(7)
test1.print()
# [1, 4, 7]