Link List · Data Structures
20 questions · asked in 15 of 27 years (2000–2026)
MCQ 19 · NAT 11-mark 14 · 2-mark 6
2000Questions per year2026
Questions 1–5 of 20
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 and Answer: (B)
Count the nodes recursively: a node adds 1 to the count of the nodes after it. The function is called only on a non-empty list, so the last node is the stopping point.
E1: the base case is the last node, . Then the function returns 1, for that one node.
E2: otherwise the answer is 1 (this node) plus the size of the rest of the list, which starts at . So E2 is . This is option (B). For a list of nodes, the calls reach the last node after steps and the total is .
Why the others fail:
(A) and (D) call with the same . The argument never changes, so the recursion never ends.
(C) tests and returns 1 there. The calls go past the last node to NULL, and that NULL case adds 1 for a node that does not exist. The result is (and a one-node list would give 2).
Answer: (B)
E1: the base case is the last node, . Then the function returns 1, for that one node.
E2: otherwise the answer is 1 (this node) plus the size of the rest of the list, which starts at . So E2 is . This is option (B). For a list of nodes, the calls reach the last node after steps and the total is .
Why the others fail:
(A) and (D) call with the same . The argument never changes, so the recursion never ends.
(C) tests and returns 1 there. The calls go past the last node to NULL, and that NULL case adds 1 for a node that does not exist. The result is (and a one-node list would give 2).
Answer: (B)
Let LIST be a datatype for an implementation of linked list defined as follows:
L1: 1 → 7 → 12 → 3 → 9 → 5 → 11 → 15 → 8 → NULL
L2: 1 → 11 → 6 → 9 → 15 → 12 → 4 → NULL
Consider the following C program segment that modifies the list L1. The number of nodes in L1 after the execution of the code segment is _________. (Answer in integer)
typedef struct list {
int data;
struct list *next;
} LIST;
Suppose the program has created two linked lists, L1 and L2, whose contents are given below (code for creating L1 and L2 is not provided here). L1 contains 9 nodes, and L2 contains 7 nodes.L1: 1 → 7 → 12 → 3 → 9 → 5 → 11 → 15 → 8 → NULL
L2: 1 → 11 → 6 → 9 → 15 → 12 → 4 → NULL
Consider the following C program segment that modifies the list L1. The number of nodes in L1 after the execution of the code segment is _________. (Answer in integer)
int find (int query, LIST *list) {
while (list != NULL){
if(list->data == query) return 1;
list = list->next;
}
return 0;
}
int main () {
... ... ...
ptr1=L1; ptr2=L2;
while (ptr1->next != NULL){
query = ptr1->next->data;
if (find (query, L2))
ptr1->next = ptr1->next->next;
else ptr1 = ptr1->next;
}
... ... ...
return 0;
}
Answer: 5
L2 works as a lookup set: . L1 is .
starts at the head node of L1 (value 1). The code only tests the value of the next node, so the head is never tested and stays in the list, even though 1 is in L2. If the next value is in L2, that node is unlinked and stays where it is. Otherwise moves forward.
Trace:
Next is 7: not in L2, advance.
Next is 12: in L2, delete 12.
Next is 3: not in L2, advance.
Next is 9: in L2, delete 9.
Next is 5: not in L2, advance.
Next is 11: in L2, delete 11.
Next is 15: in L2, delete 15.
Next is 8: not in L2, advance. Now is at the last node and the loop ends.
Deleted nodes: 12, 9, 11, 15, which is 4 nodes. L1 is now , so nodes.
Answer: 5
starts at the head node of L1 (value 1). The code only tests the value of the next node, so the head is never tested and stays in the list, even though 1 is in L2. If the next value is in L2, that node is unlinked and stays where it is. Otherwise moves forward.
Trace:
Next is 7: not in L2, advance.
Next is 12: in L2, delete 12.
Next is 3: not in L2, advance.
Next is 9: in L2, delete 9.
Next is 5: not in L2, advance.
Next is 11: in L2, delete 11.
Next is 15: in L2, delete 15.
Next is 8: not in L2, advance. Now is at the last node and the loop ends.
Deleted nodes: 12, 9, 11, 15, which is 4 nodes. L1 is now , so nodes.
Answer: 5
Consider an unordered list of distinct integers.
What is the minimum number of element comparisons required to find an integer in the list that is NOT the largest in the list?
What is the minimum number of element comparisons required to find an integer in the list that is NOT the largest in the list?
Answer: (A)
We only need to find some element that is surely not the largest in the whole list. We do not need to find the largest element. We also do not need to check the rest of the list.
Pick any two elements and compare them. The smaller one is guaranteed to not be the maximum of the list, because the larger one of the pair is bigger than it. This takes only one comparison. It works for any list of distinct integers.
Answer: 1
Pick any two elements and compare them. The smaller one is guaranteed to not be the maximum of the list, because the larger one of the pair is bigger than it. This takes only one comparison. It works for any list of distinct integers.
Answer: 1
Let SLLdel be a function that deletes a node in a singly-linked list given a pointer to the node and a pointer to the head of the list. Similarly, let DLLdel be another function that deletes a node in a doubly-linked list given a pointer to the node and a pointer to the head of the list.
Let n denote the number of nodes in each of the linked lists. Which one of the following choices is TRUE about the worst-case time complexity of SLLdel and DLLdel?
Let n denote the number of nodes in each of the linked lists. Which one of the following choices is TRUE about the worst-case time complexity of SLLdel and DLLdel?
Answer: (D)
To delete a node, we have to change the next pointer of its predecessor.
- Singly linked list: the only way to find the predecessor is to walk from the head .
- Doubly linked list: the prev pointer of the node gives the predecessor right away .
Answer: SLLdel is O(n) and DLLdel is O(1)
- Singly linked list: the only way to find the predecessor is to walk from the head .
- Doubly linked list: the prev pointer of the node gives the predecessor right away .
Answer: SLLdel is O(n) and DLLdel is O(1)
Consider the problem of reversing a singly linked list. To take an example, given the linked list below,
the reversed linked list should look like
Which one of the following statements is TRUE about the time complexity of algorithms that solve the above problem in O(1) space?
the reversed linked list should look like
Which one of the following statements is TRUE about the time complexity of algorithms that solve the above problem in O(1) space?
Answer: (A)
Go through the list one time using three pointers (prev, curr, next). Reverse each link as you move:
next = currnext; currnext = prev; prev = curr; curr = next.
Every node is visited only once, and just 3 extra pointers are used. So the time is and the space is . It cannot be faster, because every link has to be changed.
Answer: The best algorithm needs time in the worst case
next = currnext; currnext = prev; prev = curr; curr = next.
Every node is visited only once, and just 3 extra pointers are used. So the time is and the space is . It cannot be faster, because every link has to be changed.
Answer: The best algorithm needs time in the worst case
Keep practising