Skip to content
Official PYQ · 2026

GATE CS 2026 Set 1 Q29 — Linked Lists

Explain with AI

Consider the following code snippet in C language that computes the number of nodes in a non-empty singly linked list pointed to by the pointer variable head.

struct node{
    int elt;
    struct node *next;
};
int getListSize (struct node *head)
{
    if( E1 ) return 1;
    return E2;
}

Which one of the following options gives the correct replacements for the expressions E1\textbf{E1} and E2?\textbf{E2}?

Choose one option
View worked solution

Answer: B

To find the size of a non-empty singly linked list recursively, we define a base case and a recursive step:

  • Base Case: Since the list is guaranteed to be non-empty, the smallest possible list has exactly 11 node. For a single-node list, the next pointer is NULL (i.e., head->next == NULL). In this case, the function should return 11. Thus, E1 must be head->next == NULL.
  • Recursive Step: If the list has more than one node, the total size is 11 (for the current head node) plus the size of the remaining list starting from the next node. Thus, E2 must be 1 + getListSize(head->next).

Let's verify why the other options are incorrect:

  • Option A & D: The recursive call uses head instead of head->next, which results in infinite recursion (stack overflow).
  • Option C: If E1 is head == NULL, then a list with 11 node would call 1 + getListSize(NULL). Since getListSize(NULL) returns 11 (due to the base case), the total size returned for a 11-node list would be 22, which is incorrect.

Therefore, the correct replacements are given by Option B.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Linked Lists questions