Javascript 我的LinkedList删除方法有什么问题
我的删除功能有什么问题, 我可以移除头部,但不能移除任何其他节点Javascript 我的LinkedList删除方法有什么问题,javascript,data-structures,linked-list,Javascript,Data Structures,Linked List,我的删除功能有什么问题, 我可以移除头部,但不能移除任何其他节点 SLinkedList.prototype.remove=函数(值){ 如果(!this.findValue(value))返回; if(this.head.value===值){ this.head=this.head.next; 返回; } var-prev=null; var cur=this.head; while(当前值!==值){ prev=cur; cur=cur.next } 上一个=当前下一个; }您的fine
SLinkedList.prototype.remove=函数(值){
如果(!this.findValue(value))返回;
if(this.head.value===值){
this.head=this.head.next;
返回;
}
var-prev=null;
var cur=this.head;
while(当前值!==值){
prev=cur;
cur=cur.next
}
上一个=当前下一个;
}
您的fineValue()方法中有两个错误。首先,你的while循环在看这个头,它从来没有改变过。第二,在第一次迭代后返回false。希望这有帮助
SLinkedList.prototype.findValue = function(value) {
if (!this.head) return false
var cur = this.head;
while (cur) { //Need to check cur not this.head
if (cur.value === value) {
return true
}
cur = cur.next
//return false; //Move this out of the while loop
}
return false
}
上面的代码似乎对应于
SLinkedList.prototype.findValue
,而不是SLinkedList.prototype.remove
,该函数正在检查链接列表中是否存在该值。您可以点击“repl it”链接查看完整的实现。。。移除方法(每个代码)只移除head值或head.next值(如果您修复了检查this.head.next.value==value的条件)。但它并没有走得更远。因此,也许您想首先遍历列表并查找元素,其中next.value是value.Thank刚刚意识到我输入了错误的snipped,只是更改了它