I am trying to construct a linked list using a Class
with JS. Inside my Class
I have a function called checkForEmptyNext()
which traverses the list and searches for the node with a next:
property of null
The function should recursively call itself until it finds the correct node
LinkedList
// 10 --> 15 --> 16
class LinkedList {
constructor(value){
this.head = {
value: value,
next: null
}
this.tail = this.head;
this.length = 1;
}
append(value){
if(this.head.next === null){
this.head.next = {
value,
next: null
}
this.tail = this.head.next;
this.length++
}else{
this.checkForEmptyNext(value, this.head.next)
}
}
checkForEmptyNext(value, node){
if(node.next === null){
node.next = {
value,
next: null
}
this.tail = node.next;
this.length++
}else{
this.checkForEmptyNext(value, node.next)
}
}
}
const linked = new LinkedList(10);
linked.append(5)
linked.append(16)
console.log(linked)
The console.log is showing:
LinkedList {
head: { value: 10, next: { value: 5, next: [Object] } },
tail: { value: 16, next: null },
length: 3 }
Why am I seeing [Object]
instead of the actual serialized value?