我有以下代码:
def popLast(aLinkedList):
ptr = aLinkedList
while ptr != None:
if ptr['next']['next'] == None and ptr['next'] != None:
del_node = ptr['data']
ptr['next'] = ptr['next']['next']
ptr = ptr['next']
return (del_node,ptr)
当我把
myLinkedList = createList(['a', 'b', 0, [1, 2]])
for i in range(getLength(myLinkedList)):
(a, myLinkedList) = popLast(myLinkedList)
print(a, end = ", new list: ")
printList(myLinkedList)
它一直在给我
UnboundLocalError:局部变量' del_node'在分配前引用
当我使用popLast(myLinkedList)
时,没有问题。一切正常。我不知道是不是因为我的回归元组是问题所在。
答案 0 :(得分:1)
如果传入空列表,则永远不会初始化del_node。这是造成错误的原因。要更改此设置,请首先将del_node初始化为None:
def popLast(aLinkedList):
ptr = aLinkedList
del_node = None
while ptr is not None:
...