Created
May 23, 2024 04:35
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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