Reverse a list without making a tempList

Is it possible to recursively reverse a list if you can't make a temp list?  All I have access to is a pointer to the list.  I can traverse the list and print it out in reverse but I can't seem to be able to actually store it in reverse order.
Here is how I print the list in reverse order.  Can someone help me with the logic to actually change the data?  Thanks.
void AuxPrintReverse(NodeType* myPtr, int i)
{
     if(myPtr != NULL)
     {
          AuxPrintReverse(myPtr->next, i+1);
          if( i == 10)
               cout << endl;
          cout << myPtr->info << " ";
     }
}
freedumb87Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SalteCommented:
If you can print it in reverse you can store it in reverse.

Without making a temporary list though is perhaps not quite true, you make so many recursive calls that the temporary list is stored in the stack frames of all those function instantiations :-)

But yeah, if we ignore that you really store the temporary list in such a way yes.

It is in fact easy to do also, I believe I have done enough hints...

I could show you how to reverse a list in LISP. This is done in exactly the manner which you are seeking but I think that would be too much of a spoiler.

How do you reverse an empty list?

How do you reverse a list with n+1 elements?

Alf
0
substandCommented:
you could do something like:

void ReverseLinkedList(NodeType* nodeToReverse, NodeType* reverseTo, int totalNodesToReverse)
{

if (totalNodesToReverse > 0)    
{
NodeType* nextNodeToReverse=nodeToReverse->next;
NodeType* reverseNextNodeTo=nodeToReverse;

nodeToReverse->next=reverseTo;
totalNodesToReverse--

ReverseLinkedList(nextNodeToReverse, reverseNextNodeTo, totalNodesToReverse);
}

}

call it initially like:

ReverseLinkedList(myPtr, NULL, 10);
0
Mayank SAssociate Director - Product EngineeringCommented:
>> If you can print it in reverse you can store it in reverse.

That is the most correct comment posted on this page yet and I don't understand why you need to reverse it if you can actually access it in reverse order anyway.

However, assuming that you need to do so, here is a non-recursive implementation:

// assuming 'start' points to the first node

NodeType p, q, r ;
p = start ;
q = p -> next ;

while ( q != NULL )
{
  r = q -> next ;
  q -> next = p ;
  p = q ;
  q = r ;

} // end while

start = p ;

Hope that helps!

Mayank.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.