Removing a Value
Let’s implement remove.
public boolean remove(T value) {
Node<T> current = head.next;
while (current != tail) {
if (Objects.equals(current.value, value)) {
current.prev.next = current.next;
current.next.prev = current.prev;
size--;
return true;
}
current = current.next;
}
return false;
}
The traversal is the same linear scan as indexOf. When it finds a match, we unlink the node from its neighbors. Because of the sentinels, we never have to check for a null prev or next.