Linked List Delete

Remove the first node matching a value by relinking its predecessor past it.

Time O(n) Space O(1) Singly Linked List delete

How it works

Remove the first node matching a value by relinking its predecessor past it.

Implementation

function sllDeleteOp(state, target) {
  const valueArr = state.value;
  const next = state.next;
  let current = state.head;
  let prev = -1;
  while (current !== -1) {
    visitNode(current);
    compareKeys(current, target);
    if (valueArr[current] === target) {
      if (prev === -1) {
        state.head = next[current];
      } else {
        next[prev] = next[current];
        linkNodes(prev, next[current]);
      }
      removeNode(current);
      finish(current);
      return;
    }
    prev = current;
    current = next[current];
  }
  reportNotFound();
}