2016-06-21 75 views
-1

我创建了一个私人节点删除方法,但我不断收到错误,说该方法必须返回节点类型。有人能帮我吗?删除方法和节点

private Node remove(Node n){ 

    Node current = first; 
    Node pre=null; 

    w 
    } 
} 
+0

由于返回类型是Node,所以必须返回一个Node。 – theVoid

+0

提示:在方法结束时'current!= null'会发生什么?在这种情况下该方法返回什么? – Jesper

回答

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 
} 


} 
0

你的方法的返回类型为类型节点

private Node remove(Node n) 

如果你没有覆盖的情况下你不想返回任何东西,将返回类型改为void 否则返回当前建议的节点。

if(current==null){ 
     return null; 
    } else { 
     pre.next = current.next; 
     return current; // the node which was remove 
    }