I am getting illegal constructor when creating a new Node
.
I have tried doing it with array and it works but whenever I use Node
it just gives me an error saying illegal instructor.
Here is my code:
class SinglyLinkedList {
constructor(){
this.head = null;
this.tail = null;
this.length = 0;
}
push(value)
{
const newNode = new Node(value)
if(this.head===null){
this.head = newNode
this.tail = this.head
}
else{
this.tail.next = newNode
this.tail = this.tail.next
}
this.length+=1
return this
开发者_StackOverflow }
}
const list = new SinglyLinkedList()
list.push('Hi')
list.push('this')
list.push('is')
list.push('Aman')
console.log(list.tail)
Node() is undefined that's why it is an illegal constructor.
Add this to your code.
class Node {
constructor(value) {
this.value = value;
}
}
精彩评论