In theory
The most important thing is, that the list invariants stays intact after the deletion. That is
- there are no circles,
- each node points to the next node (unless the node is the last one),
- in a doubly linked list, each node is the successor of its predecessor (unless the node is the first one),
- in a doubly linked list, each node is the predecessor of its successor (unless the node is the last one),.
The list invariants do not say anything about nodes that do not belong to the list, so it does not really matter whether or not you set current.next = null
during the deletion.
In parctice
Leaving current.next
as it is might hinder automatic garbage collection, because references to objects might exist, that are no longer needed. But this depends on the exact circumstances.
In languages without automatic garbage collection, the concept of owning another object exists. An object that owns another object is responsible for managing the resources of that other object (e.g. the memory that the other object occupies). When the owning object is deleted, the owning must delete the owned object. In such a case, if you do not set current.next = null
before deleting current
, other objects are deleted that should not have been deleted.