GATE CS 2026 Set 1 | Question 39

Programming & Data Structures › Arrays & Linked Lists · MCQ · 2 marks · Answer: B

  1. Question 39Programming & Data Structuresmcq2 marks
    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?
    • A.
      E1: head == NULL E2: 1 + getListSize(head)
    • B.
      E1: head->next == NULL E2: 1 + getListSize(head->next)
      ✓
    • C.
      E1: head == NULL E2: 1 + getListSize(head->next)
    • D.
      E1: head->next == NULL E2: 1 + getListSize(head)

    Answer: B

    Solution

    Answer: (B) The base case returns 1, so it must fire at the last node, where head->next == NULL. Otherwise, count this node and recurse on the rest. • (A), (D): recursing on head itself never ends. • (C): returns 1 for NULL, which counts one node too many (a 3-node list gives 4).

More GATE PYQ on Arrays & Linked Lists

All GATE CS Programming & Data Structures PYQ →

Attempt GATE CS 2026 Set 1 in real GATE exam mode

Full paper with a timer, the GATE interface and a topic-wise analysis — free.

Create free account →