{"record":{"id":"792dc948452d9b11","repo":"krahets/hello-algo","slug":"stack-is-empty-792dc9","errorCode":null,"errorMessage":"Stack is empty","messagePattern":"Stack is empty","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"en/codes/python/chapter_stack_and_queue/linkedlist_stack.py","lineNumber":47,"sourceCode":"\n    def push(self, val: int):\n        \"\"\"Push\"\"\"\n        node = ListNode(val)\n        node.next = self._peek\n        self._peek = node\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"Pop\"\"\"\n        num = self.peek()\n        self._peek = self._peek.next\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"Access top of the stack element\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Stack is empty\")\n        return self._peek.val\n\n    def to_list(self) -> list[int]:\n        \"\"\"Convert to list for printing\"\"\"\n        arr = []\n        node = self._peek\n        while node:\n            arr.append(node.val)\n            node = node.next\n        arr.reverse()\n        return arr\n\n\n\"\"\"Driver Code\"\"\"\nif __name__ == \"__main__\":\n    # Initialize stack\n    stack = LinkedListStack()\n","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/python/chapter_stack_and_queue/linkedlist_stack.py#L29-L65","documentation":"LinkedListStack.peek raises IndexError('Stack is empty') when size() is zero, preventing the self._peek.val dereference on a None top pointer. Because pop() calls peek() first, popping an empty stack surfaces the same exception. The guard gives a clear message and avoids an AttributeError on the None head.","triggerScenarios":"Calling peek() or pop() on an empty stack; calling after the top node was popped; unmatched push/pop in expression evaluation; drain loops that over-pop.","commonSituations":"DFS/recursion emulation via explicit stack; bracket matching; monotonic-stack comparisons that peek before pushing; undo inspection on an empty history.","solutions":["Guard the access: `if not stack.is_empty(): top = stack.peek()`.","Drive drains with `while not stack.is_empty()`.","Use stack.size() to bound counted loops.","Wrap peek in a helper returning Optional for empty-state callers."],"exampleFix":"// before\ntop = stack.peek()  # raises if empty\n// after\ntop = stack.peek() if not stack.is_empty() else None","handlingStrategy":"validation","validationCode":"if not stack.is_empty():\n    top = stack.peek()","typeGuard":"def stack_nonempty(s) -> bool:\n    return not s.is_empty()","tryCatchPattern":"try:\n    top = stack.peek()\nexcept IndexError:\n    top = None","preventionTips":["Guard peek in monotonic-stack comparisons with is_empty().","Drive drains with `while not stack.is_empty()`.","Wrap peek in an Optional-returning helper."],"tags":["stack","linkedlist","indexerror","empty-state","python","data-structures"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}