Basically you don't need this line:
head=temp;
Notice, that "temp" always points to last node (called 4 by you), because you iterate up to next==NULL in while loop. There is no point in holding "temp" value.
When your recursion hits first condition:
head->next==NULL
it returns to the previous call, sets "next" pointer of the "next" node to current node (so '4' is pointing to '3'), and then sets current's node "next" pointer to NULL (this is important, because it hits '1' it has to point to NULL).
You can delete some of your code, this should do the trick:
void reverseLLrecursion(node* &head){
if(head->next==NULL){
return ;
}
else
{
reverseLLrecursion(head->next);
head->next->next=head;
head->next=NULL;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…