← back to the blog


Linked list in JavaScript code example step by step

Posted on December 1st, 2019 in JavaScript by George
// Implement class Linked List

function LinkedList() {
    // Define default state
    let length = 0,
        head = null;

    //implement Node class

    var Node = function(element) {
        this.element = element;
        this.next = null;
    };

    //Define get method for the head  
    this.getHead = function() {
        return head;
    };

    //Define get method for the size of the list
    this.getSize = function() {
        return length;
    };


    this.remove = function(element) {

        if (!head) {
            return false;
        }

        if (!head.next && head.element === element) {
            head = null;
            length = 0;
        }

        let holdElement = head;
        let templength = length;

        while (holdElement && templength > 0) {
            if (holdElement.element === element) {
                holdElement.element = holdElement.next && holdElement.next.element ? holdElement.next.element : null;
                holdElement.next = holdElement.next.next ? holdElement.next.next : null;
                --length;
                --templength;
            } else {
                --templength;
            }
            holdElement = holdElement.next ? holdElement.next : null;
        }

        // Only change code above this line
    };

    this.indexOf = (element) => {
      let currentNode = head;
      let index = 0;
      if (element === head.element) {
        return index;
      }

      while (currentNode.element !== element) {
        currentNode = currentNode.next;
        ++index;
      }
    return index;
  }

  this.elementAt = (index) => {
      let tIndex = 0;
      if (index === 0) return head.element;

      let currentNode = head;
      while (currentNode.next) {

      if (index === tIndex) {
        return currentNode.element;
      }
        ++tIndex;
        currentNode = currentNode.next;
      }
  }

    //Implement node set method
    this.add = function(element) {
        // Only change code below this line
        if (!head) {
            length += 1;
            head = new Node(element);
        } else {
            length += 1;
            if (!head.next) {
                head.next = new Node(element)
            } else {
                let currentNode = head; // get hold of the first node in the head

                //iterate the nodes list until we get to the last node (having the next method returning null)
                while (currentNode.next) {
                    currentNode = currentNode.next;
                }
                //got the last node in the list ? ok assign to next the new node
                currentNode.next = new Node(element);
            }
        }
    };

    this.removeAt = (index) => {  
      var currentNode = head;
      var previousNode;
      if(index < 0 || index >= length) {
        return null;
      }

    if(head && !head.next) {
      let temp = head.element;
      head = null;
      length = 0;
      return temp;
    }

    let tTemp = 0;

    while(index !== tTemp) {
      ++tTemp;
      previousNode = currentNode;
      currentNode = currentNode.next ? currentNode.next : null;
    }
    previousNode.next = currentNode.next ? currentNode.next : null;
    --length;
    return currentNode.element;
  }
}


// Test initialise new list object
let newList = new LinkedList();

//Do different operations
newList.add('Nr1'); //add first node
console.log(newList.getHead()); //console output

newList.add('second level');
console.log(newList.getHead().next); //console output

newList.add('3rd level')
console.log(newList.getHead().next.next); //console output

newList.remove('Nr1'); //remove first element

console.log(newList.getHead(), 'head'); //console output