Revisit delete
- Implement the core operations of List efficiently.
Open the DoublyLinkedList.java
file and find the delete
method.
public void delete(Position<T> target) {
Node<T> targetNode = convert(target);
Node<T> beforeTarget = targetNode.prev;
Node<T> afterTarget = targetNode.next;
beforeTarget.next = afterTarget;
afterTarget.prev = beforeTarget;
}
Exercise The implementation above fails to account for some edge cases! Identify those edge cases.
Solution
Here are some edge cases:
- If
target
is the first element, we will not have abeforeTarget
. - If
target
is the last element, we will not have aafterTarget
. - If
target
is the only element, we will not have abeforeTarget
nor aafterTarget
.
In every case above, we need to treat with care and also make sure to update the head
and/or tail
pointers.