2016-06-21 79 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 
    }