# Data Structure Questions and Answers-Length of a Linked List using Recursion

## Click on any option to know the CORRECT ANSWERS

 Question 1
Consider the following iterative implementation used to find the length of a linked list:

struct Node { int val; struct Node *next; }*head; int get....len() { struct Node *temp = head->next; int len = 0; while(.....) { len++; temp = temp->next; } return len; }

Which of the following conditions should be checked to complete the above code?

 A temp->next != 0 B temp == 0 C temp != 0 D none of the mentioned

Question 1 Explanation:
The condition "temp != 0" should be checked to complete the above code.

 Question 2
What is the output of the following code?

#include<stdio.h> #include<stdlib.h> struct Node { int val; struct Node *next; }*head; int get....len() { struct Node *temp = head->next; int len = 0; while(temp != 0) { len++; temp = temp->next; } return len; } int main() { int arr[10] = {1, 2, 3, 4, 5},  n = 5,  i; struct Node *temp,  *newNode; head = (struct Node*)malloc(sizeof(struct Node)); head->next = 0; temp = head; for(i=0; i<n; i++) { newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->val = arr[i]; newNode->next = 0; temp->next = newNode; temp = temp->next; } int len = get....len(); printf("%d", len); return 0; }
 A 4 B 5 C 6 D 7

Question 2 Explanation:
The program prints the length of the linked list, which is 5.

 Question 3
What is the time complexity of the above iterative implementation used to find the length of a linked list?
 A O(1) B O(n) C O(n2) D O(logn)

Question 3 Explanation:
The time complexity of the above iterative implementation used to find the length of a linked list is O(n).

 Question 4
What is the output of the following code?

#include<stdio.h> #include<stdlib.h> struct Node { int val; struct Node *next; }*head; int get....len() { struct Node *temp = head->next; int len = 0; while(temp != 0) { len++; temp = temp->next; } return len; } int main() { int arr[10] = {1, 2, 3, 4, 5},  n = 5,  i; struct Node *temp,  *newNode; head = (struct Node*)malloc(sizeof(struct Node)); head->next = 0; int len = get....len(); printf("%d", len); return 0; }
 A 0 B Garbage value C Compile time error D Runtime error