Skip to content

Instantly share code, notes, and snippets.

@another-guy
Created May 23, 2024 04:35
Show Gist options
  • Save another-guy/98d0843b03991e2744869f11b2ac9439 to your computer and use it in GitHub Desktop.
Save another-guy/98d0843b03991e2744869f11b2ac9439 to your computer and use it in GitHub Desktop.
LeetCode: linked-list-based stack, more performant than default JS's array-based one
function createStack() {
let top = { next: undefined, value: undefined };
function isEmpty() { return top.next === undefined; }
function push(v) { top = { next: top, value: v }; }
function pop() {
if (isEmpty()) return undefined;
const { value } = top;
top = top.next;
return value;
}
function peek() { return top.value; }
function clear() { return createStack(); }
return { isEmpty, push, pop, peek, clear };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment