【问题标题】:Inserting node at tail efficiently in linked list for Python在 Python 的链表中有效地在尾部插入节点
【发布时间】:2020-04-10 12:58:21
【问题描述】:

我正在尝试在 O(1) 时间内实现一个单向链表,insertsdeletes 到双方。为此,我保存了指向 headtail 的指针。

我遇到的麻烦是我的insert_tail 方法。这是我的伪代码:

If there is no head or tail,
    Set the head AND tail to the new Node
Otherwise,
    Insert the new node as a child of the tail
    Set the tail to that new node

这是我的 Python 3 代码:

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

    def __str__(self):
        if self.next:
            return "{} -> {}".format(self.val, self.next.val)
        else:
            return "{}".format(self.val)


class LinkedList():
    def __init__(self, init):
        self.head = Node(init)
        self.tail = Node(init)

    # Search at O(n)
    def search(self, val) -> Node:
        pass

    # Insert head or tail at O(1). Returns new node.
    def insert_head(self, val) -> None:
        inserting = Node(val)
        inserting.next = self.head

        self.head = inserting

        # Inserting to empty case
        if self.tail == None:
            self.head = inserting
            self.tail = inserting

        return self.head

    def insert_tail(self, val) -> None:
        inserting = Node(val)

        # Inserting to empty case
        if self.head == None and self.tail == None:
            self.head = inserting
            self.tail = inserting

        else:
            # Change the value of the tail
            self.tail.next = inserting
            self.tail = self.tail.next

    # Insert average case O(n) + 1
    def insert(self, val) -> Node:
        pass

    # Delete at O(1).
    def delete(self, val) -> None:
        pass

    def __str__(self):
        return str(list(self))

    def __iter__(self):
        node = self.head
        while node:
            yield node.val

            node = node.next


# 14, 12, 11, 19, 17, 16, 30, 18, 22, 21, 24, 23, 15
linked = LinkedList(30)

linked.insert_head(16)
linked.insert_head(17)
linked.insert_head(19)
linked.insert_head(11)
linked.insert_head(12)
linked.insert_head(14)
print(linked)

linked.insert_tail(18)
linked.insert_tail(22)
linked.insert_tail(21)
linked.insert_tail(24)
linked.insert_tail(23)
linked.insert_tail(15)
print(linked) # Should actually be: [14, 12, 11, 19, 17, 16, 30, 18, 22, 21, 24, 23, 15]
# But instead it printsas if a new tail was never inserted: [14, 12, 11, 19, 17, 16, 30]

【问题讨论】:

  • 要在O(1)时间内对两边进行删除,你需要一个双向链表;否则在删除后找到新的tail 需要从头部迭代。见en.wikipedia.org/wiki/Doubly_linked_list
  • 你是对的:我误解了插入的含义是 O(1)。虽然插入是恒定的,但遍历不是。由于我没有对以前的参考,如果我什至设法让insert_tail 工作,我将无法实现delete_tail。更多信息:stackoverflow.com/questions/840648/…
  • @kaya3 如果您想提交它作为答案,我会批准它。感谢您的宝贵时间。
  • The trouble I'm running into is my insert_tail method 不,不是。你没有关注Set the head AND tail to the new Node的信:self.head = self.tail = Node(init)

标签: python algorithm data-structures linked-list singly-linked-list


【解决方案1】:

要在 O(1) 时间内对两边进行删除,您需要一个 doubly-linked list;否则在删除后找到新的尾部需要从头部迭代。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 2011-08-13
    • 2016-10-11
    • 2020-11-15
    • 2021-05-23
    相关资源
    最近更新 更多