← DiffPush

Implement Stack using Linked List

Baseline Stack and Queues · Learning O(1) per operation · O(n)

The Climbing Rope of Cargo Tags

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.

Constraints

Examples

Example 1

Input: {"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.

Example 2

Input: {"operations":[["pop"],["push",3],["pop"]]}
Output: [-1,3]
Removing from an empty rope yields -1 before any tag exists.

Solve this in your browser →

Also on LeetCode ↗