how its ans is option c
Quiz on ink list
Please paste the question here.
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<<” “; } Java void fun(node start) { if(start == null) return; System.out.print(start.data+” “); if(start.next != null ) fun(start.next.next); System.out.print(start.data+” "); } 1 4 6 6 4 1 1 3 5 1 3 5 1 2 3 5 1 3 5 5 3 1
Answer:1 3 5 5 3 1
fun() prints alternate nodes of the given Linked List, first from head to end, and then from end to head. If Linked List has even number of nodes, then skips the last node.
You can also refer this
after printing 1 3 5 head is pointing to 5 and when we do head->next->next it becomes null
so basically we are calling fun(NULL)so it should return and out shoud only be 135
Have you read the discussion whose link i have mentioned above. Please ready that. I have explained clearly how output is 1 3 5 5 3 1