【发布时间】:2020-06-16 19:58:11
【问题描述】:
class Node:
def __init__(self,data):
self.data = data
self.address = None
class LinkedList:
def __init__(self):
self.start = None
def insert_at_position(self,position,data):
node = Node(data)
i = 0
temp = self.start
while(i<position-1 and temp!=None):
i +=1
temp = temp.address
t1 = node
t1.address = temp
temp = t1
【问题讨论】:
-
你能发布你的完整代码吗?
-
好吧,
temp = t1将新节点分配给一个临时变量。您需要在插入位置之前更新节点的self.start或address。您还需要确定当position大于列表时会发生什么。
标签: python algorithm data-structures linked-list singly-linked-list