I have the next implementation of Linked List:
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedLIst {
constructor() {
this.head = {};
}
add(head) {
if(!this.head.next) {
this.head = new Node(head);
}
this.head.next = new Node(head);
}
}
const list = new LinkedLIst();
list.add(1)
list.add(2)
list.add(3)
console.log(list)
I don't understand, why 2 is not added within the list? And how to fix that?