Monday, September 3, 2012

Reverse of single linked list

Reverse a single linked list:

public void reverse()
{
   Node currentNode, nextNode, loopNode;
   if(first==null)
      return;

   currentNode=first;
   nextNode= first.next;
   loopNode=null;


   while(nextNode != null)
   {
        currentNode.next = loopNode;
        loopNode= currentNode;
        currentNode=nextNode;
        nextNode =nextNode.next;
   }

   first = currentNode;
   first.next = loopNode;

}

No comments:

Post a Comment