Quite frequently it happens that a reference to a Node in a doubly linked Node list will be available (it doesn't matter how for the present discussion) and that Node is to be deleted from the list.
For example, suppose a Node variable p is available somehow that references Node with value 10 below.
Only the two links in red need to change:
| -----> <----- |
-- |
3 | -- |
-----> <----- |
-- |
10 | -- |
-----> <----- |
-- |
15 | -- |
-----> <----- |
The code to delete Node p is easier to understand at first by introducing variables to reference the previous Node with value 3 and following Node with value 15.
These are the Nodes whose .next and .prev fields need to change respectively.
Node before = p.prev; Node after = p.next; before.next = after; after.prev = before;
| -----> <----- |
-- |
3 | -- |
-----> <----- |
-- |
15 | -- |
-----> <----- |
or the Node names before and after can be replaced by p.prev and p.next respectively to get the very short code:
p.prev.next = p.next; p.next.prev = p.prev;