Open In App

Data Structures | Linked List | Question 6

What is the output of following function in which start is pointing to the first node of the following linked list 1->2->3->4->5->6 ?




void fun(struct node* start)
{
  if(start == NULL)
    return;
  printf(\"%d  \", start->data);
  
  if(start->next != NULL )
    fun(start->next->next);
  printf(\"%d  \", start->data);
}

(A)



1 4 6 6 4 1

(B)



1 3 5 1 3 5

(C)

1 2 3 5

(D)

1 3 5 5 3 1

Answer: (D)
Explanation:

The function prints the data of the current node and then recursively calls itself with the second next node (i.e., start->next->next).

So, it prints the data of every alternate node of the linked list i.e 1 3 5, and then, since the next->next of 5 is null, it returns and prints the data of the current node, so it then prints 5 3 1.

Therefore, for the given linked list 1->2->3->4->5->6, the function would print 1 3 5 5 3 1.

Quiz of this Question
Please comment below if you find anything wrong in the above post

Article Tags :