[英]Whats wrong with my LinkedList remove method
我的删除功能出了什么问题,我可以删除头但不能删除任何其他节点。
SLinkedList.prototype.remove = function(value) { if(!this.findValue(value) )return; if(this.head.value === value){ this.head = this.head.next; return; } var prev = null; var cur = this.head; while(cur.value !== value){ prev = cur; cur = cur.next } prev.next = cur.next; }
这是完全的JavaScript实现链接REPL它
fineValue()方法中有两个错误。 首先,您的while循环正在查看this.head,它从未改变。 其次,您在第一次迭代后返回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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.