Linked list

by rishul matta

JavaScript

class Node {
	constructor(value) {
		this.next = null ;
		this.value = value;
	}

	updateNextNode(node) {
		this.next = node;
	}
}

class List {
	constructor(elements) {
		this.head = null;
		this.end = null;

		elements.forEach(val => {
			const node = new Node(val);
			
			if (this.head == null) {
				this.head = node;
        this.end = node;
				return;
			}

			
			this.end.updateNextNode(node);
      this.end = node;
		});
	}

	traverse() {
		let node = this.head;
		while(node !== null) {
			console.log(node.value);
			node = node.next;
		}

	}
  
  _printReverse(node) {
  	if(node.next) {
    	this._printReverse(node.next);
    }
    console.log(node.value);
  }
  
  reverse() {
  	this._printReverse(this.head);
  }
}
			
const list = new List([4,3,4,5,6])
			
/* list.traverse() */
list.reverse()