PegasusWang / python_data_structures_and_algorithms

Python 中文数据结构和算法教程

Home Page:http://pegasuswang.github.io/python_data_structures_and_algorithms/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

linked_list.py 中的remove操作仍然是错误的

TillLindemann opened this issue · comments

很明显,你的prevnode并没有更新,从头到尾都是指向的根节点,这样的话,你每删除一个元素,实际上就是把这个元素前面的所有元素都删了,remove方法应该这样写:
def remove(self, value): # O(n)
""" 删除包含值的一个节点,将其前一个节点的 next 指向被查询节点的下一个即可
:param value:
"""
prevnode = self.root #
curnode = self.root.next
for curnode in self.iter_node():
if curnode.value == value:
prevnode.next = curnode.next
del curnode
self.length -= 1
return 1 # 表明删除成功
else:
prevnode = curnode
return -1 # 表明删除失败

抱歉,已经修正。#3
这个 mr 我并没有仔细检查和调整单测就 merge 了。已经修改并且重新完善单测。感谢提出问题,后续我会修改讲义和重新剪辑视频相关内容。防止产生误导。