I was practicing my coding chops today and solving the "remove all elements of a certain value from a linked list" problem today. The solution I came up with was
public void RemoveAll ( T val )
{
if(_root == null)
return;
if(_root.Value == val)
{
_root = _root.Next;
RemoveAll(val);
}
Node last = _root,
cur = _root.Next;
while(cur != null)
{
if(cur.Value == val)
last.Next = cur.Next;
else
last = cur;
cur = cur.Next;
}
}
and here's my question:
When cur.Value == val
I'm doing something like changing the list from
A -> B -> C
to
A -> C
Will the compiler or run-time environment see that B
is no longer in use and dispose of it? Or should I do that explicitely?
I have a second question which is whether a call stack blows up for recursive void
methods. As you see here, there is a chance of the method calling itself. But since it's a method that doesn't return a value, can't the run-time environment just wipe the data about the last call? There is no reason for it to remain in memory (right?).