Why is it than we can use singly-linked list to represent a stack only if the links between nodes are oriented to point in the direction of top (newer) to bottom (older)?
2 Answers
Okay i get it now, removing the head of a singly linked list take O(1) time and removing the tail takes O(n) time since you have to do a linear search to find the node that points to the tail and make that node point to null. So if the stack was tail to head (each pointer points up towards top of stack) it would take O(n) every time you popped as opposed to O(1) for a stack thats implemented head to tail (each pointer points to bottom of stack).