Log in

View Full Version : Any C# gurus around?


daft
05-28-07, 12:00 PM
If so, you might be able to help out a bit. I'm trying to remove the last node from a linked list, and it works alright until I only have one node remaining in the list. That's were all goes wrong. This is the code I have for the GetLast() - method:

public LNode<T> GetLast()
{
if (head == null)
{
return null;
}
else
{
LNode<T> current = head;

while (current.Next != null)
{

current = current.Next;

}

LNode<T> toGet = current;
current.Previous.Next = null;
return toGet;
}
}
I know why the last element isn't fetched, it doesn't have a previous node to refer to, so the current.Previous.Next doesn't work. I've stared myself blind at this problem and really would appreciate any halp I can get since Google didn't turn up anything. I tried checking if current.Next == null, but that didn't help. :cry: Sorry for the confused description, just ask if somethings unclear. :)

daft
05-28-07, 12:22 PM
Never mind, I read my post twice and managed to find a solution. :)