我创建了一个私有Node删除方法,但我不断收到错误,说该方法必须返回Node类型。有人能帮助我吗?
private Node remove(Node n){
Node current = first;
Node pre=null;
w
}
}
答案 0 :(得分:0)
当您找到要移除的节点时,您没有覆盖案例
private Node remove(Node n){
Node current = first;
Node pre=null;
while(current!=null && !current.equals(n)){
pre=current;
current=current.next;
}
if(current==null){
return null;
} else {
//found the node to remove. Connect the pre with the next node, and
//return the current node
}
}
答案 1 :(得分:0)
您的方法的返回类型是节点
类型 private Node remove(Node n)
如果您不想退货,请将退货类型更改为无效 否则按照建议返回当前节点。
if(current==null){
return null;
} else {
pre.next = current.next;
return current; // the node which was remove
}