Count the number of nodes in the linked list which is not empty? struct node { int value; struct node * next; } fun (node * head) { if (E1) return 1; else E2; }
Solution :
The correct option is: E1: head → next == Null, E2: 1 + fun (head → next);
Let us analyze the structure of the recursive function and determine how to count the nodes of a non-empty linked list. The skeleton of the function is:
struct node {
int value;
struct node * next;
};
fun (node * head) {
if (E1) return 1;
else E2;
}
1. Determining the Base Case (E1):
Since the problem states that the linked list is not empty, the minimum number of nodes in the list is 1. The base case of the recursion should handle the smallest possible input, which is a list containing exactly one node. In a singly linked list, the last node (or a single-node list) is characterized by having its next pointer point to Null. Thus, the condition to check if we are at the last node is:
E1: head → next == Null
When this condition is met, the function correctly returns 1.
2. Determining the Recursive Step (E2):
If the current node is not the last node (i.e., head → next != Null), the total number of nodes starting from the current node is 1 (for the current node itself) plus the number of nodes in the remainder of the list. The remainder of the list begins at the next node, head → next. Therefore, the recursive step must call fun on head → next and add 1 to the result:
E2: return 1 + fun(head → next);
3. Illustrative Trace:
Consider a linked list with two nodes: [Node 1] → [Node 2] → Null.
- We call fun(Node 1). Since Node 1 → next is Node 2 (not Null), the base case E1 is false. We execute E2, which evaluates to:
1 + fun(Node 2)
- In the recursive call fun(Node 2), Node 2 → next is Null. The base case condition E1 (head → next == Null) is true, so it returns 1.
- Substituting this back into the original call, the final expression evaluates to:
This correctly yields a count of 2 nodes.
Completely free for students. Educators & coaching institutes get 2 months free Pro trial to create tests.
Access unlimited practice questions, detailed step-by-step solutions, and exam study materials — completely free for students.
Create and conduct professional online assessments with AI question generation, tab proctoring, and comprehensive batch management.
A comprehensive CUET UG Mathematics Question Paper with Solutions – 2 June 2023 (Shift 3) designed to help students practice and understand the actual CUET exam pattern. This paper covers important topics from Algebra, Calculus, Coordinate Geometry, Probability, Statistics, Matrices, Determinants, and Vectors, featuring a balanced mix of conceptual, numerical, and application-based questions. Detailed solutions are provided to strengthen problem-solving skills, improve conceptual clarity, and enhance calculation accuracy. Based on the CUET syllabus and previous year exam trends, this paper is ideal for practice, revision, self-assessment, time management, previous year analysis, and final preparation for upcoming CUET UG Mathematics examinations.
A comprehensive CUET Accountancy Question Paper – 20 June 2023 (Shift 2) designed to help students practice and analyze previous year exam patterns for effective CUET preparation. This paper covers important topics including Accounting for Partnership Firms, Company Accounts, Financial Statements Analysis, Cash Flow Statements, Accounting Ratios, and Fundamental Accounting Principles. It features a balanced mix of conceptual, numerical, and application-based questions to strengthen accounting concepts, improve calculation accuracy, and enhance problem-solving skills. Based on the CUET exam structure and syllabus, this paper is ideal for practice, revision, self-assessment, time management, previous year analysis, and final preparation for upcoming CUET Accountancy examinations.
Copyright © 2026 Combe Labs Private Limited. All Rights Reserved.