在python中插入链表的开头

在python中插入链表的开头,python,data-structures,linked-list,nodes,Python,Data Structures,Linked List,Nodes,我正在尝试创建一个函数,它接受链表的头和一个值,创建一个具有该值的节点,将node.next设置为head,然后将新节点更新为head。我让它工作了,但我觉得我通过返回值来做这件事效率很低,所以我想知道是否有更优雅的方法来做这件事 以下是我使用的代码: #!/usr/bin/env python3 """ This module is a linked list implementation. """ class Node: """ Node structure to be u

我正在尝试创建一个函数,它接受链表的头和一个值,创建一个具有该值的节点,将node.next设置为head,然后将新节点更新为head。我让它工作了,但我觉得我通过返回值来做这件事效率很低,所以我想知道是否有更优雅的方法来做这件事

以下是我使用的代码:

#!/usr/bin/env python3
"""
This module is a linked list implementation.
"""

class Node:
    """
    Node structure to be used in our linked list.
    """
    def __init__(self, value):
        self.value = value
        self.next = None

def linked_insert(head, value):
    """
    Insert new node to the tail of the linked list.
    Time Complexity: O(n)
    """
    current = head
    while current.next is not None:
        current = current.next
    current.next = Node(value)

def linked_insert2(head, value):
    """
    Insert new node to the head of the linked list, making sure to update head.
    Time Complexity: O(1)
    """
    to_insert = Node(value)
    to_insert.next = head
    head = to_insert
    return head


def linked_extract(head):
    """
    Extract the last element in the linked list
    """
    current = head

    while current.next.next is not None:
        current = current.next

    tail = current.next
    current.next = None

    return tail


def linked_display(head):
    """
    Print all node values in the linked list
    """
    current = head
    while current is not None:
        print(current.value)
        current = current.next


# Test Program
head = Node(5)
# linked_insert(head, 1)
# linked_insert(head, 2)
# linked_insert(head, 3)
#
# print(linked_extract(head).value)

head = linked_insert2(head, 1)
head = linked_insert2(head, 2)
head = linked_insert2(head, 3)
linked_display(head)
有没有一种方法可以在不必返回
head
的值并在测试程序中设置
head
=返回值的情况下执行此操作

所讨论的函数是
链接的\u insert2()
。整个程序将是python中的链表实现

使用链表类实现:

#!/usr/bin/env python3

"""
This module is a linked list implementation.
"""

class Node:
    """
    Node class to be used in our linked list.
    """
    def __init__(self, value):
        self.value = value
        self.next = None


class Linkedlist:
    """
    Linked list implementation that supports functions including inserting
    at head, inserting at tail, extracting elements, and displaying all
    elements in the ist
    """
    def __init__(self, head):
        self.head = Node(head)

    def insert(self, value):
        """
        Insert new node to the tail of the linked list.
        Time Complexity: O(n)
        """
        current = self.head
        while current.next is not None:
            current = current.next
        current.next = Node(value)

    def insert2(self, value):
        """
        Insert new node to the head of the linked list, making sure to update head.
        Time Complexity: O(1)
        """
        to_insert = Node(value)
        to_insert.next = self.head
        self.head = to_insert


    def extract(self):
        """
        Extract the last element in the linked list
        """
        current = self.head

        while current.next.next is not None:
            current = current.next

        tail = current.next
        current.next = None

        return tail


    def display(self):
        """
        Print all node values in the linked list
        """
        current = self.head
        while current is not None:
            print(current.value)
            current = current.next


# Test Program
head = Linkedlist(5)
# linked_insert(head, 1)
# linked_insert(head, 2)
# linked_insert(head, 3)

head.insert2(1)
head.insert2(2)
head.insert2(3)
head.display()

您可以拥有一个链表对象,其中的函数作为其方法。这样,插入时就不必保留头部。但是,为什么还要麻烦呢,这似乎已经足够好了。@YSA如果这个插入是O(1),那么保留对head的引用并没有什么错。@YSA:我不知道如果不保留指向head的指针,在C中该怎么做?你也需要这么做,我明白你的意思。不用担心,这在Python中是完全可以的。阅读以下内容以更好地了解正在发生的事情:。Python的级别比C.@YSA高得多,因为它在
self
中保留了head引用。如果你向我展示你的新实现,我会帮上更多的忙。