删除节点时替换方法是什么样的?
最佳答案
以下逻辑应该有效:
if (position.c > 0) {
position.c--;
if (position.c == 0) {
// delete entire node
if (previous == null) {
// delete head node
head = position.next;
} else {
// delete a node with a predecessor
previous.next = position.next;
}
}
}
请注意,我已将您代码中的 next
分配颠倒过来。您所做的就是在 position
处使用循环引用终止列表(因为此时 previous.next ==position
)。
顺便说一句,这里不需要 if
语句:
found = true;
if(found==true){
. . .
}
它可能只是:
found = true;
. . .
关于java - 如何删除链表中的节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16096745/