0

I am trying to pop an item off a stack (using a linked list as oppose to an array). I first created a LinkedList class with 3 nodes with the values [1,2,3]. So I would like pop off the last node (node_C, value=3), therefore I expect to see the values [1,2]. Instead nothing prints out.

class LinkedList:
    def __init__(self, value):
        self.value = value
        self.next = None

node_A = LinkedList(1)
node_B = LinkedList(2)
node_C = LinkedList(3)

node_A.next = node_B
node_B.next = node_C

def pop(head):
    current_node = head
    while current_node.next:
        if current_node.next == None:
            break
        else:
            current_node = current_node.next

del current_node

return node_A.value, node_B.value, node_C.value

try:
    print(pop(node_A))
except NameError:
    pass

How can I rewrite this to achieve my desired results (i.e. show values 1,2 .. with 3 popped off)?

2
  • where do you call the pop() method, I don't see it being called anywhere Commented Sep 27, 2017 at 14:13
  • Last 4 lines of code Commented Sep 27, 2017 at 14:20

2 Answers 2

1

The del current node and return node_A.value, node_B.value, node_C.value commands should belong to the pop() function, so they should be indented. But anyway the del current node doesn't work for me. Instead you could write current_node.value = None but then you still return all 3 node values so the result would be 1,2,None.

I would rather write the pop() function inside the class and add another printlist() function to the class as well. The pop() function just removes the last element from the list (changes the next attribute to None for the 2nd last element in the list) and doesn't print or return anything. The printlist() function iterates through the list and prints out all elements (while there are next elements). Here is my code:

class LinkedList:
    def __init__(self, value):
        self.value = value
        self.next = None

    def pop(self):
        current_node = self
        while current_node.next:
            if current_node.next.next == None:
                current_node.next = None
            else:
                current_node = current_node.next

    def printlist(self):
        current_node = self
        lst = [current_node.value]
        while current_node.next:
            current_node = current_node.next
            lst.append(current_node.value)
        print lst


node_A = LinkedList(1)
node_B = LinkedList(2)
node_C = LinkedList(3)

node_A.next = node_B
node_B.next = node_C

try:
    node_A.pop()
    node_A.printlist()
except NameError:
    pass

If I run this, the result is [1,2]. If I remove the node_A.pop() I get [1,2,3]. If I write another node_A.pop() then result is [1].

Sign up to request clarification or add additional context in comments.

1 Comment

Works great! Thanks
1

I guess I have found the issue with your logic, so based on the code provided it seems that pop function doesn't return anything, may be it's just formatting or something else.

But here is the correct version of your code, where I just delete the last node in the pop method and I call another method called listValues which returns me with the node values that exist in the linked list after pop

Look at the below implementation for a clearer view.

class LinkedList:
    def __init__(self, value):
        self.value = value
        self.next = None

node_A = LinkedList(1)
node_B = LinkedList(2)
node_C = LinkedList(3)

node_A.next = node_B
node_B.next = node_C

def pop(head):
    current_node = head
    while current_node.next:
        if current_node.next == None:
            del current_node
            break
        else:
            current_node = current_node.next

def listValues(head):
  values = []
  current_node = head
  while current_node.next:
    values.append(current_node.value)
    current_node = current_node.next
  return values

try:
    pop(node_A)
    print(listValues(node_A))
except NameError:
    pass

Hope this helps!

3 Comments

Thanks, but I am trying to pop from a stack using a "Linked List" object. Your solution uses an Array (i.e. list.append).
Look closely the pop method does what it needs to do, the listValues method is just use for printing out the values
the pop method removes the last element present in the linked list, I am using another helper method to list out the values present in the linked list as the list might grow to become 100 or so

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.