A <-> B <-> C <-> D <-> A…
// A is firstNode, D is lastNode
if ( length == 1 )
{
firstNode = null;
lastNode = null;
firstNode.next = null;
firstNode.prev = null;
}
else
{
Node secondNode = firstNode.next;
Node secondToLast = lastNode.prev;
firstNode.next = null;
firstNode.prev = null;
lastNode.next = null;
lastNode.prev = null;
secondNode.prev = null;
secondToLast.next = null;
firstNode = null;
lastNode = null;
}
That should send everything in between as candidates for garbage collection, I hope?
As long as there are no strong references that refer to the nodes, then it doesn’t matter if there are circular references “between” the nodes, the objects will still be candidates for garbage collection.
That means when there are no more local variables or static variables that hold a reference to the objects (whether they fall out of scope, you explicitly set them to
nullor assign them to something else) then the object will be garbage collected.