Python链表移除末尾节点

Python Linked list removal of node at the end

我正在 Python 3.6.5 中实现 LinkedList。我坚持从最后删除一个节点:

代码如下:

class Node(object):

def __init__(self, data):
    self.data = data
    self.nextNode = None

class LinkedList(object):

def __init__(self):
    self.head = None
    self.counter = 0

def print_list(self):
    current = self.head
    while current is not None:
        print(current.data)
        current = current.nextNode

def size(self):
    print(self.counter)

def insert_end(self,data):
    newNode = Node(data)
    current = self.head
    if self.head is None:
        self.head = newNode
        self.counter+=1
    else:
        while current.nextNode is not None:
            current = current.nextNode
        current.nextNode = newNode
        self.counter+=1

def insert_end(self,data):
    newNode = Node(data)
    current = self.head
    if self.head is None:
        self.head = newNode
        self.counter+=1
    else:
        while current.nextNode is not None:
            current = current.nextNode
        current.nextNode = newNode
        self.counter+=1

def insert_beg(self,data):
    newNode = Node(data)
    if self.head is None:
        self.head = newNode
        self.counter+=1
    else:
        newNode.nextNode = self.head
        self.head = newNode
        self.counter+=1

def remove_beg(self):
    if self.head is None:
        print("Linked List is already empty")
    else:
        currentNode = self.head
        self.head = self.head.nextNode
        self.counter-=1
        del currentNode

def remove_end(self):
    if self.head is None:
        print("Linked List is already empty")
    else:
        current_head = self.head
        while current_head.nextNode is not None:
            current_head = current_head.nextNode
        current_node = current_head
        current_head.nextNode = None
        del current_node.nextNode
        self.counter-=1


linkedlist = LinkedList()
linkedlist.remove_beg()
linkedlist.insert_end(1)
linkedlist.insert_end(2)
linkedlist.insert_end(3)
linkedlist.insert_end(4)
print("printing list")
linkedlist.print_list()
linkedlist.insert_beg(22)
print("printing list after addition of 22 in the beginning")
linkedlist.print_list()
linkedlist.remove_beg()
print("printing list")
linkedlist.print_list()
print("printing size")
linkedlist.size()
linkedlist.insert_end(55)
linkedlist.print_list()
linkedlist.remove_end()
linkedlist.print_list()

除了从末尾删除节点外,一切正常。

输出:

Traceback (most recent call last):
File "intermediate.py", line 84, in
linkedlist.remove_end()
File "intermediate.py", line 61, in remove_end
current_head.nextNode = None
AttributeError: 'NoneType' object has no attribute 'nextNode'

要删除最后一个节点,我们需要在倒数第二个节点和None

之间创建一个link
def remove_end(self):
    if self.head is None:
        print('List is empty!')
    else:
        current_head = self.head
        current_node = None
        while current_head.nextNode is not None:
            previous = current_head
            current_head = current_head.nextNode
        previous.nextNode = None
        del current_head