【问题标题】:How to delete and return the last item in a linked list?如何删除并返回链表中的最后一项?
【发布时间】:2019-05-12 05:15:51
【问题描述】:

我的家庭作业需要我从链表中弹出最后一项,由于某种原因,一些测试用例有效,但有些无效,我不知道为什么。

class Node:
    def __init__(self, init_data):
        self.data = init_data
        self.next = None
    def get_data(self):
        return self.data
    def get_next(self):
        return self.next
    def set_data(self, new_data):
        self.data = new_data
    def set_next(self, new_next):
        self.next = new_next
    def __str__(self):
        return str(self.data)

class LinkedList:
    def __init__(self):
        self.head = None
    def add(self, item):
        new_node = Node(item)
        new_node.set_next(self.head)
        self.head = new_node

    def __str__(self):
        result = "("
        node = self.head
        if node != None:
            result += str(node.data)
            node = node.next
            while node:
                result += ", " + str(node.data)
                node = node.next
        result += ")"
        return result
    def remove_from_tail(self):
        if self.head is None:
            return None
        prev = None
        cur = self.head
        while cur.next is not None:
            prev = cur
            cur = cur.next
        if prev:
            prev.next = None
        return cur

#test case one is incorrect
my_list = LinkedList()
my_list.add(400)
print(my_list.remove_from_tail())
my_list.add(300)
print(my_list.remove_from_tail())
my_list.add(200)
print(my_list.remove_from_tail())
my_list.add(100)
print(my_list.remove_from_tail())
#should be 400 300 200 100 but instead I got 400 400 300 200

#test case two works fine
fruit = LinkedList()
fruit.add('cherry')
fruit.add('banana')
fruit.add('apple')
last_one = fruit.remove_from_tail()
print('Removed:', last_one)#got"Removed: cherry"
print(fruit)#(apple, banana)

cur = self.headself.head 在删除400 后应该指向300 时,我不知道测试用例一失败的原因是什么。所以当我返回 cur 时,它不应该打印出两个 400。任何帮助将不胜感激。

【问题讨论】:

    标签: python python-3.x linked-list nodes


    【解决方案1】:

    您的代码很好,问题是您在每次添加后都调用remove_from_tail,但是您要做的是调用add直到添加所有元素,然后一一调用remove_from_tail,这工作正常

    my_list = LinkedList()
    my_list.add(400)
    my_list.add(300)
    my_list.add(200)
    my_list.add(100)
    print(my_list.remove_from_tail())
    print(my_list.remove_from_tail())
    print(my_list.remove_from_tail())
    print(my_list.remove_from_tail())
    

    这个输出

    400
    300
    200
    100
    

    您的add 函数也有点奇怪,通常我们修复头部,并且对于每个新元素,遍历到列表的末尾并附加它,但是在您的add 中,您正在更新@987654328 上的每个元素@ 和更新头。

    而您的 remove_from_tail 假设头部是固定的,我们正在遍历到最后然后更新,这实际上发生了

    所以在你运行 add 4 次后,列表是

    head
    100 -> 200 -> 300 -> 400
    

    如你所料,尾部相应地被删除,从400100

    同样在您的remove_from_tail 函数中,您需要处理列表中仅保留一个元素并且您想要删除它的情况,一旦您添加原始测试用例也可以工作

    def remove_from_tail(self):
        #If linked list is empty return None
        if self.head is None:
            return None
        #If only one element in list, return that element and set head to None
        elif self.head.next is None:
            cur = self.head
            self.head = None
            return cur
    
        #Otherwise iterate through the list
        prev = None
        cur = self.head
        while cur.next is not None:
            prev = cur
            cur = cur.next
        if prev:
            prev.next = None
        return cur
    

    然后打印原始测试用例

    400
    300
    200
    100
    

    还有

    【讨论】:

    • 因为 add 函数需要我在链表的前面添加每个新元素,这就是 self.head 不固定的原因。另外,有什么方法可以让代码在不改变测试用例的情况下仍然打印正确的答案??
    • 一般add 添加到最后一个元素而不是前面,你有特殊情况吗@Victoriavv 好的,让我更新我的答案来解决这个问题
    • 酷!检查我更新的答案!你错过了在你的函数中考虑一个案例@Victoriavv
    猜你喜欢
    • 1970-01-01
    • 2013-08-12
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 2013-12-16
    • 1970-01-01
    • 2017-01-14
    相关资源
    最近更新 更多