Question

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 and E2?

Choose one option
Official answer and solution
Official answer
B
From
Official GATE 2026 answer key

Solution

Check the base case return value (return 1;) for a non-empty singly linked list.

Because the base case if (E1) return 1; returns 1 (the count for a 1-node list), the condition E1 must check whether head points to the last node of the list, which is head->next == NULL. Otherwise, the function adds 1 for the current node to the size of the remaining sublist: E2 is 1 + getListSize(head->next).

Final answer

Option (B)

Common trap

Choosing E1: head == NULL (Option C), which would return 1 for the empty tail after the last node and overcount the list size by 1.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2026 Computer Science and Information Technology - Session 1 (CS1)
Question
39 of 65
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.