There are many posts with probably with same question but the problem says it has to be done by
node* reverseList (node * lh)
{
if(lh==NULL)............ ;
else if (lh->next==NULL)...........;
else ...........;
}
the three blanks must be filled
the first two are simply
return NULL
and
return lh
repectively
one way could be just to go down and reverse the pointers but in that case how can i keep the tail intact even after backtracking? is it possible at all?
The trick to solving recursive problems is to pretend that you are done already. To solve this homework by yourself, you need to answer three questions:
lhset toNULL)?You answered the first two already:
NULLandlhare the right answers. Now think of the third one:At this point,
reversedTailcontains pre-reversed tail of your list. All you need to do is set lh->next to NULL, add it to the back of the list that you are holding, and returnreversedTail. The final code looks like this: