我有一个关于更新的问题自尾在我的双链表类中,专门用于add\u to\u head方法。我写了一个单独的函数来做这个,但我只是想知道是否有一个更有效的方法来做它。 以下是我所拥有的:
def add_to_head(self, data):
'''(input)-> None
takes any type of input and creates a node with that input as data.
Takes that node and adds it to the front of the list
'''
new_node = DLLNode(data)
if self.head == None:
self.head = self.tail = new_node
else:
self.head.prev = new_node
new_node.next = self.head
self.head = new_node
self.update_tail()
def update_tail(self):
'''(DLLList)->None
updates the tail of the DLLList
'''
current = self.head
while current != None:
current = current.next
current = self.tail
这是假设自尾以及自首创建此类时已设置为“无”。另外,我调用这个函数的唯一一次是在调用add\u to\u head方法时,因为当我添加到列表的末尾时,我已经在更新尾部了。 我还能用什么方法来实现这一点?你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐