正如前面提到的代码审查:
https://codereview.stackexchange.com/questions/162021/improving-java-linklist-implementation
答案 0 :(得分:1)
您可以查看泛型。 (google it - 这在这里解释得太多了)
然后在某些时候,你会为oneBeforeLast
使用额外的对象。
你可以尝试这样的事情:
public void deleteLast(){
if(!isEmpty()){
Node tmp = first;
while (tmp.next != null && tmp.next.next != null){
tmp = tmp.next;
}
tmp.next = null;
}
}
另一点是:你有没有使用last
变量?我不这么认为,在单链表中没有什么用处。 (唯一的原因可能是创建方法getLast()
。)
请尽量避免使用空行和如此大的主要方法来缩短代码示例;)