删除节点时替换方法会是什么样子?
答案 0 :(得分:1)
以下逻辑应该有效:
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;
. . .