Open In App
Related Articles

Data Structures | Linked List | Question 1

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

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 05 Jan, 2013
Like Article
Save Article
Similar Reads
Complete Tutorials