0
0
DSA Pythonprogramming

Pop Operation on Stack in DSA Python

Choose your learning style9 modes available
Mental Model
A stack is like a pile of plates where you only take the top plate off. Pop means removing the top item.
Analogy: Imagine a stack of books on a table. You can only remove the top book first before taking any below it.
Top
 ↑
[3] -> [2] -> [1] -> null
Dry Run Walkthrough
Input: stack: 3 -> 2 -> 1 -> null, pop operation
Goal: Remove the top element (3) from the stack and show the new top
Step 1: Check if stack is empty
3 -> 2 -> 1 -> null
Why: We must ensure there is something to pop
Step 2: Remove the top element (3)
[pop->3] 2 -> 1 -> null
Why: Pop removes the top element and returns it
Step 3: Update top pointer to next element (2)
Top ↑ 2 -> 1 -> null
Why: The new top is now the element below the popped one
Result:
Top ↑ 2 -> 1 -> null
Popped element: 3
Annotated Code
DSA Python
class Node:
    def __init__(self, value):
        self.value = value
        self.next = None

class Stack:
    def __init__(self):
        self.top = None

    def push(self, value):
        new_node = Node(value)
        new_node.next = self.top
        self.top = new_node

    def pop(self):
        if self.top is None:
            return None  # stack empty
        popped_value = self.top.value
        self.top = self.top.next  # move top down
        return popped_value

    def __str__(self):
        curr = self.top
        values = []
        while curr:
            values.append(str(curr.value))
            curr = curr.next
        return ' -> '.join(values) + ' -> null'

# Driver code
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
print(stack)  # before pop
popped = stack.pop()
print(f"Popped element: {popped}")
print(stack)  # after pop
if self.top is None:
check if stack is empty before popping
popped_value = self.top.value
store the top element's value to return
self.top = self.top.next
update top pointer to next element, removing the current top
OutputSuccess
3 -> 2 -> 1 -> null Popped element: 3 2 -> 1 -> null
Complexity Analysis
Time: O(1) because pop removes the top element directly without traversal
Space: O(1) as no extra space is needed for pop operation
vs Alternative: Compared to removing from an array's start which is O(n), stack pop is faster and simpler
Edge Cases
empty stack
pop returns None and does not crash
DSA Python
if self.top is None:
stack with one element
pop removes the only element and top becomes None
DSA Python
self.top = self.top.next
When to Use This Pattern
When you see a problem requiring last-in-first-out removal, think of the stack pop operation to remove the top element efficiently.
Common Mistakes
Mistake: Trying to pop from an empty stack without checking causes errors
Fix: Always check if the stack is empty before popping
Mistake: Not updating the top pointer after pop leaves the stack in an incorrect state
Fix: Update the top pointer to the next node after popping
Summary
Removes the top element from the stack and returns it
Use when you need to remove the most recently added item first
The key is to update the top pointer to the next element after removal