void fun2(struct Node* head)
{
if(head== NULL)
return;
printf("%d ", head->data);
if(head->next != NULL )
fun2(head->next->next);
printf("%d ", head->data);
}
Here,for linked list 1->2->3->4->5->6
the output should be 5 3 1, right?
void fun2(struct Node* head)
{
if(head== NULL)
return;
printf("%d ", head->data);
if(head->next != NULL )
fun2(head->next->next);
printf("%d ", head->data);
}
Here,for linked list 1->2->3->4->5->6
the output should be 5 3 1, right?
@neharika.srivastav Output should be: 1 3 5 5 3 1
The 1st printf statement before the recursive call fun2() will be responsible for printing 1 3 5
The 2nd printf statement after the recursive call fun2() will be responsible for printing 5 3 1.
Why 5 3 1… pls ans
@neharika.srivastav It is simple recursion. The printf statement before the recursive call prints the elements in normal order. You are printing before filling the call stack so it is in normal order. So 1 3 5
The printf statement after the recursive call will execute when we are returning back or when the recursive call stack is getting emptied. So it will print the same data in reverse order as 5 3 1.
Hope this helps
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.