Linked List Search

Find the first node holding a target value by walking the list from the head.

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

How it works

Find the first node holding a target value by walking the list from the head.

Implementation

function sllSearchOp(state, target) {
  const valueArr = state.value;
  const next = state.next;
  let current = state.head;
  while (current !== -1) {
    visitNode(current);
    compareKeys(current, target);
    if (valueArr[current] === target) {
      reportFound(current);
      return;
    }
    current = next[current];
  }
  reportNotFound();
}