Linked List

by sramnan

JavaScript

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

function LinkedList() {
  this.head = null;
}

LinkedList.prototype.push = function(val) {
  if (this.head === null) {
    this.head = new Node(val)
  } else {
    var current = this.head;
    while (current.next != null) {
      current = current.next;
    }
    current.next = new Node(val)
  }
}

var ll = new LinkedList();
ll.push(2);
ll.push(3);
ll.push(4);
console.log(ll.head);