Monday, November 16, 2009

c# revert a LinkedList

void ReverseLinkedList(LinkedList ls)
{
if ( ls == null || ls.Head == null )
return;
ListNode head = ls.Head;
ListNode current = head.Next;
while(current!=null)
{
ListNode tmp = current;
current = current.Next;
tmp.Next = head;
head = tmp;
}
ls.Head.Next = null;
ls.Head = head;
}