Hi guys,
I was wondering if I could get some help on the topic of recurrsion. A week ago i was told to create a application using nodes. this week I need to convert the reverse_list fuction I made below into using recursion. any help would be great thanks.

not using recurrsion and works!

void reverse_list (node*& head_ptr)
{ 	
node* temp_ptr = head_ptr->link();	// holding 2nd node reference.
node* iter_ptr = temp_ptr->link();              // hold the reference of 3rd Node OR NULL.
head_ptr->set_link(0);		// setting the first Node next = 0
while (temp_ptr==NULL)		// looping through 2nd node to the end.
                {
	    iter_ptr = temp_ptr->link();		// saving the 3rd Node.
	    temp_ptr->set_link(head_ptr);	// reversing
	    head_ptr = temp_ptr;		// incrementing head.
	    temp_ptr = iter_ptr;		// increment
	}
}

trying to use recurrsion and does not work

void reverse_list (node*& head_ptr)
{  
node* current_ptr = head_ptr->link();                         //holding 2nd node
while (current_ptr->link() != NULL)		//checking if at end of list
	{
	current_ptr->set_link(head_ptr);	                //reversing
	head_ptr = current_ptr;		//incrementing
	reverse_list(head_ptr);	                //calling function again
	}
}

I was able to figure out another way to reverse the linked list using recursion.

node* reverse_list (node* head_ptr)
	{  
		node* current = NULL;						
		if (head_ptr->link() != NULL)				
			{
				current = reverse_list(head_ptr->link());	
				(head_ptr->link())->set_link(head_ptr);		 
				head_ptr->set_link(NULL);					
			}
		else
			{
				current = head_ptr;
			}
	return current;
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.