awesome-everything RU
↑ Back to the climb

Algorithms from zero

Lists, stacks, queues: code reading

Crux Read real JavaScript snippets — a reversal pointer bug, a delete-by-value bug, a broken array queue, and a monotonic-stack pattern — and pick the highest-leverage diagnosis or fix.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at middle altitude — in the sky
◷ 14 min

Pointer bugs in linked-list code do not crash loudly — they corrupt the structure quietly. Read each snippet, trace the pointers in your head, and pick what a senior engineer would flag first.

Goal

Practise the loop you run on every list/stack/queue review: read the pointer moves, predict where a node is lost or a complexity claim is wrong, and name the precise fix.

Snippet 1 — the reversal that loses the tail

function reverse(head) {
  let prev = null;
  let current = head;
  while (current !== null) {
    current.next = prev;   // reverse the pointer
    prev = current;
    current = current.next; // advance
  }
  return prev;
}
Quiz

What happens when this runs on [10] -> [20] -> [30]?

Snippet 2 — delete by value without a sentinel

function deleteValue(head, target) {
  let current = head;
  while (current !== null) {
    if (current.value === target) {
      current = current.next; // "remove" it
    }
    current = current.next;
  }
  return head;
}
Quiz

Does this delete the target node from the list?

Snippet 3 — the array queue under load

class Queue {
  constructor() { this.items = []; }
  enqueue(x) { this.items.push(x); }
  dequeue() { return this.items.shift(); }
}
// Driver: stream of 1,000,000 enqueue/dequeue pairs
Quiz

The queue is correct but a load test shows it crawling. What is the cause and the highest-leverage fix?

Snippet 4 — the monotonic-stack pattern

function dailyWarmerWait(temps) {
  const result = new Array(temps.length).fill(0);
  const stack = []; // indices, decreasing temps
  for (let i = 0; i < temps.length; i++) {
    while (stack.length && temps[i] > temps[stack[stack.length - 1]]) {
      const j = stack.pop();
      result[j] = i - j;   // days until a warmer temperature
    }
    stack.push(i);
  }
  return result;
}
Quiz

For temps [73, 74, 75, 71, 69, 72, 76, 73], what does this return and what is its complexity?

Recap

Every list/stack/queue bug is read in the pointers and the cost model. Reversal must save the successor before overwriting current.next, or it loses the tail. Deletion must repoint the predecessor — moving a local reference changes nothing — and a sentinel head erases the head special case. A FIFO queue must never dequeue with shift; use head/tail pointers or a circular buffer for O(1). And the monotonic stack stays linear because each index is pushed once and popped at most once, regardless of the inner while loop.

Continue the climb ↑Lists, stacks, queues: build a sequence toolkit
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources3
expand
  1. 01
  2. 02
  3. 03

Trademarks belong to their respective owners. Editorial reference only.