DLL Operation: Delete
Suppose we want to delete the node pointed to by the reference variable target
:
![](/img/14/2020-10-05-08-44-18.png)
Exercise Complete the implementation of delete
.
public void delete(Node<T> target) {
// TODO Implement me!
}
Hint: Use the following visualization as guidance.
![](/img/14/2020-10-05-08-44-45.png)
Solution
public void delete(Node<T> target) {
Node<T> prevNode = target.prev;
Node<T> nextNode = target.next;
prevNode.next = nextNode;
nextNode.prev = prevNode;
// no need for these!
target.next = null;
target.prev = null;
}
Caution: the implementation above fails to account for edge cases!