Doubly List Insert Head

Insert a new node at the head of a doubly linked list, wiring both next and prev pointers.

Time O(1) Space O(n) Doubly Linked List insertHead

How it works

Insert a new node at the head of a doubly linked list, wiring both next and prev pointers.

Implementation

function dllInsertHeadOp(state, value) {
  const valueArr = state.value;
  const next = state.next;
  const prev = state.prev;
  const id = valueArr.length;
  valueArr[id] = value;
  next[id] = state.head;
  prev[id] = -1;
  insertNode(id, value);
  if (state.head !== -1) {
    prev[state.head] = id;
    linkNodes(id, state.head);
  }
  state.head = id;
  finish(id);
}