LINKED LIST QUIZ

Predict the output that the following code gives for a linked list given as 1->2->3->4->5->6
cpp

void fun(node* start)
{
if(start == NULL)
return;
cout<data<<" “;
if(start->next != NULL )
fun(start->next->next);
cout<data<<” ";

I get that 1->3->5 will be there. But how will the reverse order be printed after this?

Hey
When we reach node with 5 then here it will again call for start->next->next ,which will simply return because it is NULL
From there control is back to call with node 5
Them last statement prints value 5
From where control goes to call with node 3
And it prints value 3
And then control goes back to node 1 where it prints 1

Fun(1)->print(1)->fun(3)->print(3)->fun(5)->print(5)->fun(null)->return to fun(5)-> print(5)-> return to fun(3)->print(3)->return to fun(1)-> print(1)
I hope this resolves ur query