Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Data Structures | Linked List | Question 1

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

What does the following function do for a given Linked List with first node as head?

void fun1(struct node* head)
  if(head == NULL)
  printf(\"%d  \", head->data);


Prints all nodes of linked lists


Prints all nodes of linked list in reverse order


Prints alternate nodes of Linked List


Prints alternate nodes in reverse order

Answer: (B)


This function fun1 recursively prints the elements of a linked list in reverse order.

It first checks if the head is NULL, and if it is, it returns without doing anything. Otherwise, it calls the fun1 function recursively with the next node in the linked list (head->next). This continues until the last node is reached, which is the node whose next pointer is NULL.

At this point, the function starts returning, and as it returns it prints the value of each node in the linked list starting from the last node, working its way backwards to the first node. This is achieved through the printf statement which prints the value of head->data.

Thus, when this function is called with the head of a linked list as an argument, it will print the values of the nodes in reverse order.

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

My Personal Notes arrow_drop_up
Last Updated : 05 Jan, 2013
Like Article
Save Article
Similar Reads
Related Tutorials