JSFiddle - React, Tailwind, and code Playground

by Eugen Sunic

JavaScript

function Node(val) {
  this.value = val;
  this.next = null;


}

function LinkedList() {
  this.head = null;
  this.size = 0;


  this.add = function(val) {
    var node = new Node(val);
    if (!this.head) {
      this.head = node;
      return;
    }

    let current = this.head;
    while (current.next) {
      current = current.next;
    }

    current.next = node;
  }

  this.insertAt = function(element, index) {
    var counter = 0;
    let previous = this.head;
    let current = this.head.next;
    while (current) {
      if (counter === index) {
        break;
      }
      previous = current;
      current = current.next;
      ++counter
    }
    current = {
      val: 1000
    };
  }

  this.print = function() {
    let current = this.head;
    while (current) {
      console.log(current);
      current = current.next;
    }
  }

}

const linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(3);
linkedList.add(4);

console.log('bam')

linkedList.print();