How to reverse a singly linked list

//
// iterative version
//
Node* ReverseList( Node ** List )	
{

	Node *temp1 = *List;
	Node * temp2 = NULL;
	Node * temp3 = NULL;

	while ( temp1 )
	{
		*List = temp1; //set the head to last node		
temp2= temp1->pNext; // save the next ptr in temp2
		temp1->pNext = temp3; // change next to privous
		temp3 = temp1;
		temp1 = temp2;
	}
 
	return *List;
}
Chitra
Chitra

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in