Implement stack using Linkedlist
Pattern: Implementation
The head of a singly linked list is the top. Push makes a new node pointing at the old head; pop moves head to head.next. Both are O(1), and the stack never needs resizing.
The problem
Build a class LinkedListStack with push(x), pop(), top() and isEmpty(), where the items live in linked
nodes rather than an array.
On an empty structure, pop, top and peek return -1. This is this guide's own rule; the linked page does not say what happens.
Think first
- Which end of a singly linked list can you add to and remove from in O(1)?
- What does a push create?
- What does an empty stack look like?
Hints
Brute force, and why it fails
Using the tail as the top makes pop O(n), because removing the last node needs the one before it.
The key insight
In a singly linked list only the head end supports both adding and removing in O(1), so the head is the top.
Dry run
| list | null |
|---|
Empty: head is null.
The plan
push(x): head = Node(x, next = head)
pop(): if head is null: return -1
x = head.val; head = head.next; return x
top(): return -1 if head is null else head.val
isEmpty(): return head is null
Complexity
- Time
O(1) per operation- Space
O(n)
Each operation changes one pointer. Memory is one node per item.
Edge cases
- Pop on an empty stack:
-1. - Pushing after the stack became empty again.
- In C++, free the removed node.
I've built my logic, show the code
Java
class LinkedListStack {
private static class Node {
int val;
Node next;
Node(int val, Node next) { this.val = val; this.next = next; }
}
private Node head = null; // the top of the stack is the head of the list
public void push(int x) {
head = new Node(x, head); // insert at the head: O(1)
}
public int pop() {
if (head == null) return -1; // this guide's rule for an empty stack
int x = head.val;
head = head.next;
return x;
}
public int top() {
return head == null ? -1 : head.val;
}
public boolean isEmpty() {
return head == null;
}
}C++
class LinkedListStack {
public:
struct Node {
int val;
Node* next;
Node(int v, Node* n) : val(v), next(n) {}
};
Node* head = NULL; // the top of the stack is the head of the list
void push(int x) {
head = new Node(x, head); // insert at the head: O(1)
}
int pop() {
if (head == NULL) return -1; // this guide's rule for an empty stack
Node* old = head;
int x = old->val;
head = old->next;
delete old;
return x;
}
int top() {
return head == NULL ? -1 : head->val;
}
bool isEmpty() {
return head == NULL;
}
};Python
from lib import ListNode
class LinkedListStack:
def __init__(self):
self.head = None # the top of the stack is the head of the list
def push(self, x: int) -> None:
self.head = ListNode(x, self.head) # insert at the head: O(1)
def pop(self) -> int:
if self.head is None:
return -1 # this guide's rule for an empty stack
x = self.head.val
self.head = self.head.next
return x
def top(self) -> int:
return -1 if self.head is None else self.head.val
def isEmpty(self) -> bool:
return self.head is NoneCommon mistakes
- Losing the rest of the list by setting
head = new Node(x)without linking the old head. - Reading
head.valafter movinghead.