Skip to content

Instantly share code, notes, and snippets.

@amcgowanca
Created March 1, 2013 23:57
Show Gist options
  • Save amcgowanca/5068900 to your computer and use it in GitHub Desktop.
Save amcgowanca/5068900 to your computer and use it in GitHub Desktop.
A sloppy LinkedList<TVal>.Remove(ILinkedNode<int, TVal> node) method.
public bool Remove(ILinkedNode<int, TVal> node)
{
if (this.Count.Equals(0))
{
return false;
}
bool removed = false;
if (this.First.Equals(node))
{
this.First = this.First.Next;
}
else if (this.Last.Equals(node))
{
this.Last = this[this.Count - 2];
this.Last.Next = null;
}
else
{
ILinkedNode<int, TVal> previous = null;
ILinkedNode<int, TVal> current = this.First; // skip the first, its not needed, already tested
while (null != current && !removed)
{
if (current.Equals(node))
{
current = current.Next;
if (null != previous)
{
previous.Next = current;
}
removed = true;
break;
}
previous = current;
current = current.Next;
}
}
this.Count = 0;
ILinkedNode<int, TVal> n = this.First;
while (null != n)
{
n.Key = this.Count;
n = n.Next;
this.Count++;
}
return removed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment