0

I have the following code:

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)

When I put

myLinkedList = createList(['a', 'b', 0, [1, 2]])
for i in range(getLength(myLinkedList)):
    (a, myLinkedList) = popLast(myLinkedList)
print(a, end = ", new list: ")
printList(myLinkedList)

It keeps giving me

UnboundLocalError: local variable 'del_node' referenced before assignment

When I just use popLast(myLinkedList), there is no problem. Everything works fine. I don't know whether it is because my returning tuple is the problem.

2
  • The reference to createlist is not explained in your code. Please give us a Minimal, Complete, and Verifiable example. Commented Nov 12, 2016 at 0:56
  • 1
    Note: PEP8 suggests that you should use while ptr is not None instead of while ptr != None. Commented Nov 12, 2016 at 0:57

1 Answer 1

1

If an empty list is passed in, then del_node is never initialized. This is what is causing the error. To change this, first initialize del_node to None:

def popLast(aLinkedList):
    ptr = aLinkedList
    del_node = None
    while ptr is not None:
    ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.