GATE CSE 2026 Session 1 Q39: Recursive length of a non-empty singly linked list
- Programming and Data Structures
- 2 marks
- MCQ
- −2/3 if wrong
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?
Try it: multiple choice: exactly one correct option
Show answer and explanation
Answer: B (official GATE 2026 final answer key)
Concept used: Recursion on a non-empty list: base case is the last node, which counts as 1.
First step: Returning 1 is right only when head is the last node, i.e. head->next == NULL; the recursion must move to head->next.
Full step-by-step solution
Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.
Share this question
Source: official GATE 2026 Session 1 CS question paper and final answer key, organised by IIT Guwahati. Question paper · Answer key
More Programming and Data Structures PYQs
- 2026 Session 1 · Q23Non-leaf indices in a 1-indexed binary heap with n odd
- 2026 Session 1 · Q24Empty slots after linear-probing inserts with h(x) = (x+7) mod 11
- 2026 Session 1 · Q33Maximum height of a full binary tree with 23 nodes
- 2026 Session 1 · Q34Output of a C function with a shadowed block variable
- 2026 Session 1 · Q40Third insertion that can never yield a complete BST of 1..15
- 2026 Session 1 · Q61Smallest n for which foo(n) = 5 with foo(bar(n))