All files / data-structure/linkedList index.ts

100% Statements 40/40
100% Branches 5/5
100% Functions 3/3
100% Lines 40/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 401x 1x 1x 5x 5x 5x 5x 6x 6x 6x 5x 5x 6x 6x 6x 1x 1x 6x 6x 6x 6x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 1x 1x
// linked list
 
const LinkedList = <T>() => {
  let head: any = null;
  let tail: any = null;
 
  const add = (data: any) => {
    const newNode = { data };
 
    if (!head) {
      head = newNode;
    }
 
    // insert by tail
    if (tail) {
      tail.next = newNode;
    }
 
    tail = newNode;
    return;
  };
 
  const getList = () => {
    let list = [];
    let current = head;
    while (current) {
      list.push(current.data);
      current = current.next;
    }
 
    return list;
  }
 
  return {
    add,
    getList,
  }
}
 
export default LinkedList;