代码包括链表(javaScript中的数据结构)中的addingToTail实现。我希望如果有人能回答为什么在下面的代码中返回的存在没有明显的区别。as return部件是可选的。
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
this.tail = null;
this._size = 0;
}
addToTail(value) {
if (this.head === null) {
this.head = addnode;
this.tail = addnode;
this._size++;
return;
}
else {
this.tail.next = addnode;
this.tail = addnode;
this._size++;
return;
}发布于 2020-12-07 04:24:10
在两种可能的条件结束时,您将从function返回。
代码#1 (您的代码,添加了缺少的分号)
addToTail(value) {
if (this.head === null) {
this.head = addnode;
this.tail = addnode;
this._size++;
return;
}
else {
this.tail.next = addnode;
this.tail = addnode;
this._size++;
return;
}
}代码#2
addToTail(value) {
if (this.head === null) {
this.head = addnode;
this.tail = addnode;
this._size++;
}
else {
this.tail.next = addnode;
this.tail = addnode;
this._size++;
}
return;
}代码#1和#2都与末尾的return are相同。
EDIT 1:更新函数以使用尾部成员。
理想情况下,此函数类似于:
addToTail(value) {
const newNode = new Node(value);
this.tail.next = newNode;
this.tail = newNode;
this._size++;
return newNode;
}如果有效,请让我知道。
另外,看看我创建的这个LinkedList类,_InsertRawTypeItem函数符合您的要求。
https://stackoverflow.com/questions/65137824
复制相似问题