Linked List Insert Head

Insert a new node at the head of a singly linked list by relinking the head pointer.

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

How it works

Insert a new node at the head of a singly linked list by relinking the head pointer.

Implementation

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