【发布时间】:2019-12-18 22:59:29
【问题描述】:
我想在链表上的特定元素(第二个 Joey)之后添加一个元素(steve)。
当前列表:Ash->joey->Alex->Cook->Joey->bing.
期望的输出:Ash->joey->Alex->Cook->Joey->steve->bing
这是我的代码:
def insert_at_same(self , newNode, data_to_check):
currentNode = self.head
temp = 0
while currentNode.next is not None:
if (temp == 1 and currentNode.data == data_to_check):
tempNode = currentNode.next
currentNode.next = newNode
newNode.next = tempNode
return
elif currentNode.data == data_to_check:
temp = temp + 1
else:
currentNode = currentNode.next
但我的输出仍然是:Ash->joey->steve->Alex->Cook->Joey->bing。
【问题讨论】:
标签: python python-3.x linked-list