从google上获得的方法有点变化:
def removeNodeAtPosition(self, position):
if self.head == None:
print("List is empty")
return
current = self.head
if position == 0:
self.head = current.next
current = None
self.size-=1
return
for i in range(position-1):
current = current.next
if current == None:
break
if current == None:
return
if current.next == None:
return
next = current.next.next
current.next = None
current.next = next
if position == self.size: #two lines here used for updating self.tail
self.tail = current
self.size-=1
标题主要是这样写的:当我用这个方法删除节点的尾部时,我的self.tail值没有改变。我不知道我的两行代码是不是放错了地方,还是代码错了。我保留了最近一次的尝试。任何帮助解决这个问题都将不胜感激
考虑删除
current.next
而不是删除current
。这样,如果current.next
是最后一个元素,那么current
就是新的tail
相关问题 更多 >
编程相关推荐