Baseline Stack and Queues · Learning O(1) per operation · O(n)
A harbor crane hangs inspection tags on a rope: every new tag clips directly onto the rope's head, and removals always take the current head tag. Because each tag links only to the one beneath it, both actions are a single pointer update. Replay a log of clip/remove commands and report what each removal yields.
Input: A list of operations, each either ["push", value] to attach or ["pop"] to remove the head.
Output: Return the list of values produced by every pop operation, in order; a pop on an empty rope yields -1.
1 <= number of operations <= 10^5-10^9 <= pushed values <= 10^9The structure is a singly linked list with head insertion and head removalInput: {"operations":[["push",10],["push",20],["pop"],["push",30],["pop"],["pop"]]}
Output: [20,30,10]
Head insertion means the most recent tag is always removed first.
Input: {"operations":[["pop"],["push",3],["pop"]]}
Output: [-1,3]
Removing from an empty rope yields -1 before any tag exists.