如果某个节点的值在某个范围内(大于或等于low且小于或等于high),我试图从链表中删除该节点。此代码仅删除范围内找到的第一个值。
public void removeData(E low, E high) {
Node previousNode = root;
Node deleteNode = previousNode.getNext();
while (deleteNode != null) {
if (deleteNode.getValue().compareTo(low) >= 0 && deleteNode.getValue().compareTo(high) <= 0) {
previousNode.setNext(deleteNode.getNext());
}
previousNode = previousNode.getNext();
deleteNode = deleteNode.getNext();
}
}
答案 0 :(得分:0)
In this code what you need to do is:
public void removeData(E low, E high) {
Node previousNode = root;
Node deleteNode = previousNode.getNext();
while (deleteNode != null) {
if (deleteNode.getValue().compareTo(low) >= 0 && deleteNode.getValue().compareTo(high) <= 0) {
previousNode.setNext(deleteNode.getNext());
}else{
previousNode = previousNode.getNext();
}
deleteNode = deleteNode.getNext();
}
}
because if one node is deleted the previous node doesn't need to incremented just deleteNode has to. and check for another deleteNode.
Hope this may help you. Thanks!!