Snippets

Steven Berlan Interleave stack using a single queue

Created by Steven Berlan
1
2
3
4
5
6
7
function interleave(stack) {
    for (let queue = [], len = stack.length, i = 1; i < len; ++i) {
        while (stack.length > i) queue.push(stack.pop())
        while (queue.length > 0) stack.push(queue.shift())
    }
    return stack
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.