如何为双链表实现RemoveAll方法?我有一个正常的删除方法:
public boolean remove(String s){
//lists is empty
if(head==null){
return false;
}
//list has 1 element and we want to remove it
if(head.getData().equals(s) && head==tail){
head = null;
tail = null;
return true;
}
//s is at the front of the list
if(head.getData().equals(s)){
head = head.getNext();
head.setPrev(null);
return true;
}
MyDoublyNode currentNode = head;
while(currentNode.getNext()!=null &&
!currentNode.getNext().getData().equals(s)){
currentNode = currentNode.getNext();
}
//we never found it
if(currentNode.getNext()==null){
return false;
}
//trying to remove the tail
if(currentNode.getNext().getData().equals(s) &&
currentNode.getNext() == tail){
currentNode.setNext(null);
tail = currentNode;
return true;
}
//we've found what we're trying to remove, remove it
if(currentNode.getNext().getData().equals(s)){
currentNode.getNext().getNext().setPrev(currentNode);
currentNode.setNext(currentNode.getNext().getNext());
return true;
}
//never found s, return false
return false;
}
我想一举删除一个值,而不必使用一个语句调用删除超过我需要的。我还没有完全熟悉节点和双链表。