擅长:python、mysql、java
<p>这有点难搞清楚,因为你还没有发布真正的LinkedList,但是我假设你打了<code>current.get_next()</code>太多次了,你可能会到达列表的末尾。也许像这样的东西对你有魔力(<strong>更新</strong>):</p>
<pre><code>def delete(self, item):
current = self.head # we don't need the previous variable
previous = None
found = False
while current is not None and not found:
if current.get_data() == item:
found = True
else:
previous = current
current = current.get_next()
# at this point we should have either reached to end of the list
# and current is None
# or we have found the node we were looking for
if found and previous is not None:
# delete it
previous.set_next(current.get_next())
elif found and previous is None:
# removing head element
self.head = None
else:
# nothing found
print("Do whatever you want here")
</code></pre>