当位置是最后一个节点时,如何在删除特定位置节点的方法中更新self.tail?

2024-04-20 05:04:32 发布

您现在位置:Python中文网/ 问答频道 /正文

从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值没有改变。我不知道我的两行代码是不是放错了地方,还是代码错了。我保留了最近一次的尝试。任何帮助解决这个问题都将不胜感激


Tags: 方法代码selfnoneforsizereturnif