我正在尝试解决以下代码中的单链列表问题:
//piece of data -val;
//reference to next node-next;
class Node {
constructor(val) {
this.val = val;
this.next = null;
}
}
class SinglyLinkedList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
push(val1) {
var newNode = new Node(val1);
if (!this.head) {
this.head = newNode;
this.tail = this.head;
}
else
{
this.tail.next=newNode;
// this.tail=newNode
}
}
}
var list2 = new SinglyLinkedList()
list2.push("hello")
list2.push(1)
// list2.push("goodbye")
//# sourceURL=snippet:///SLL
当我尝试运行代码时,它将以错误响应:
"**Cannot access 'Node' before initialization**"
截图在这里: Screenshot
答案 0 :(得分:-1)
只需在Node
类中将node
更改为Node
,然后在push
方法中将其更改,即可使用。我不知道自己被卡在其中的原因,但是我对其进行了更改,然后才起作用。