A terminating recursive list-size function
Recursion

Answer & solution
Official answer: B
Givens and goal
For a nonempty singly linked list, getListSize(head) returns 1 if E1 is true and otherwise returns E2. Choose between base tests head==NULL or head->next==NULL, and recursive expressions 1+getListSize(head) or 1+getListSize(head->next).
Approach
Match the base return value 1 to a one-node list and ensure recursion moves to a strictly shorter suffix.
Step-by-step solution
1. The input list is nonempty, so the current head points to a node. A one-node list is recognized by a null next pointer, and its length is 1.
2. If a next node exists, the tail beginning there is also nonempty. The total length is one for the current node plus the recursively computed tail length. This supplies the base condition and recursive expression in B.
3. Induction proves correctness: length 1 returns 1; assuming the routine counts a tail of length n−1, the recursive case returns 1+(n−1)=n.
4. Passing the unchanged head recursively never decreases the problem size and does not terminate on longer lists. Using head==NULL as the base test while returning 1 gives an extra count for the empty suffix. These defects eliminate the other choices.
Assumption: As usual for a finite singly linked list, the chain is acyclic. The question's recursion would not terminate on a cyclic structure.
Trace for three nodes A→B→C→NULL: the call at C detects next==NULL and returns 1; the call at B returns 1+1=2; the call at A returns 1+2=3. With a head==NULL base returning 1, even a one-node input would instead return 1+1=2. With an unchanged-head recursive call, any list longer than one node recurses forever rather than reaching the last node.


![Question 61, GATE 2025: In C, foo(S,size) returns 0 for size=0, 1 for size=1, otherwise returns 1+foo(S+1,size−1) if S[0]≠S[1], and foo(S+1,size−1) otherwise. main prints foo(A,9) for A={0,1,2,2,2,0,0,1,1}. What is printed?](../../../assets/questions/CS-2025-S1-Q61-1.webp)










![Question 37, GATE 2016: Code: int f(int*p,int n){if(n<=1)return 0; else return max(f(p+1,n-1),p[0]-p[1]);} int main(){int a[]={3,5,2,6,4};printf("%d",f(a,5));}. max returns the greater argument. Find the printed value.](../../../assets/questions/CS-2016-S2-Q47-1.webp)








![Question 35, GATE 2010: What value does this C program print?
#include <stdio.h>
int f(int *a,int n) {
if(n<=0) return 0;
else if(*a%2==0) return *a+f(a+1,n-1);
else return *a-f(a+1,n-1);
}
int main(void) { int a[]={12,7,13,4,11,6}; printf("%d",f(a,6)); return 0; }
Options: (A) −9; (B) 5; (C) 15; (D) 19.](../../../assets/questions/CS-2010-Q45-1.webp)


![Question 75, GATE 2008: Consider these C functions, assuming valid array bounds and the intended unit-cost arithmetic model:
int f1(int n) {
if(n==0 || n==1) return n;
else return 2*f1(n-1)+3*f1(n-2);
}
int f2(int n) {
int i; int X[N],Y[N],Z[N];
X[0]=Y[0]=Z[0]=0;
X[1]=1; Y[1]=2; Z[1]=3;
for(i=2;i<=n;i++) {
X[i]=Y[i-1]+Z[i-2];
Y[i]=2*X[i]; Z[i]=3*X[i];
}
return X[n];
}
What values do f1(8) and f2(8) return?
(A) 1661 and 1640
(B) 59 and 59
(C) 1640 and 1640
(D) 1640 and 1661](../../../assets/questions/CS-2008-Q75-1.webp)
![Question 75, GATE 2008: Consider these C functions, assuming valid array bounds and the intended unit-cost arithmetic model:
int f1(int n) {
if(n==0 || n==1) return n;
else return 2*f1(n-1)+3*f1(n-2);
}
int f2(int n) {
int i; int X[N],Y[N],Z[N];
X[0]=Y[0]=Z[0]=0;
X[1]=1; Y[1]=2; Z[1]=3;
for(i=2;i<=n;i++) {
X[i]=Y[i-1]+Z[i-2];
Y[i]=2*X[i]; Z[i]=3*X[i];
}
return X[n];
}
What values do f1(8) and f2(8) return?
(A) 1661 and 1640
(B) 59 and 59
(C) 1640 and 1640
(D) 1640 and 1661](../../../assets/questions/CS-2008-Q75-2.webp)


![Question 13(a), GATE 2001: abc(s) returns at the null terminator; otherwise it calls abc(s+1) twice and then prints s[0]. main calls abc("123").](../../../assets/questions/CS-2001-Q76-1.webp)
![Question 13(a), GATE 2001: abc(s) returns at the null terminator; otherwise it calls abc(s+1) twice and then prints s[0]. main calls abc("123").](../../../assets/questions/CS-2001-Q76-2.webp)








