Store a stack as a linked list
Updated
A list-backed stack uses the head as the top. Next walks toward the bottom. Empty is null. You never walk to the tail to push.
What to focus on
The list 9 → 1 → 4 → null is stack [4, 1, 9] in our array writing (top 9). Same LIFO, different memory.
Worked examples
three
a = [4, 1, 9] as 9 → 1 → 4 → null- head 9 is top
- tail 4 is bottom
Result: top 9
one
a = [7] as 7 → null- head is top and bottom
Result: top 7
empty
a = [] as null- no head
Result: empty
Try it
Write listStack(head). head is the first node of the list-backed stack (the top), or null if empty. node.next is the next node toward the bottom, or null at the bottom. Draw [4, 1, 9] as a list with head = top. Walk next to list the buried values. Empty is null. [4, 1, 9] is 9 → 1 → 4 → null. [7] is 7 → null. [] is null.
Values are integers. You may draw an empty stack as null.