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 16–20 of 20
Let P be a singly linked list. Let Q be the pointer to an intermediate node x in the list. What is the worst-case time complexity of the best-known algorithm to delete the node x from the list ?
Answer: (A)
To delete the node x that Q points to, the node before x must have its next pointer changed to the node after x. In a singly linked list there is no pointer back, so we have to find that node by walking from the head.
Steps:
Note: we can imitate a deletion by copying the data of x's next node into x and then removing that next node in O(1). This does not delete the node x itself (the node x stays in the list and a different node is freed), so it is not counted here.
Answer: (A)
Steps:
- Find the node whose next points to ; call it . This takes O(n).
- Find the node that points to; call it . This takes O(1).
- free(Q)
Note: we can imitate a deletion by copying the data of x's next node into x and then removing that next node in O(1). This does not delete the node x itself (the node x stays in the list and a different node is freed), so it is not counted here.
Answer: (A)
Suppose each set is represented as a linked list with elements in arbitray order. Which of the operations among union, intersection, membership, cardinality will be the slowest?
Answer: (D)
Let the two sets be lists of sizes and , with elements in any order.
- Membership: scan one list until the element is found, so for that list.
- Cardinality: count the nodes of one list, so for that list.
- Union: no duplicates may remain, so each element of one set must be searched for in the other: .
- Intersection: each element of set 1 must be searched for in set 2: .
Union and intersection take product time, while membership and cardinality take linear time. So union and intersection are the slowest.
Answer: (D)
- Membership: scan one list until the element is found, so for that list.
- Cardinality: count the nodes of one list, so for that list.
- Union: no duplicates may remain, so each element of one set must be searched for in the other: .
- Intersection: each element of set 1 must be searched for in set 2: .
Union and intersection take product time, while membership and cardinality take linear time. So union and intersection are the slowest.
Answer: (D)
A circularly linked list is used to represent a Queue. A single variable p is used to access the Queue. To which node should p point such that both the operations enQueue and deQueue can be performed in constant time?
Answer: (A)
p should point to the rear node. In a circular list, is the front node, so one pointer reaches both ends in O(1).
enQueue: put the new node after the rear and make it the new rear.
If p pointed to the front node, both operations would fail to be O(1). enQueue must change the rear node's next pointer, and deQueue must change the rear node's next pointer to the new front. The rear can only be reached by walking the whole list, which is O(n). Option (D), the node next to front, has the same problem: the rear is not reachable in O(1).
Answer: (A)
enQueue: put the new node after the rear and make it the new rear.
newNode->next = p->next; // new node points to the front p->next = newNode; // old rear points to the new node p = newNode; // new node is the reardeQueue: remove the front node, so the second node becomes the front.
struct node *temp = p->next; // the front node p->next = temp->next; // rear now points to the second node free(temp);Both take a constant number of steps. (Two special cases: enQueue on an empty queue sets and ; deQueue on a one-node queue frees that node and sets p to NULL.) The extra pointers newNode and temp are only temporary; the queue itself is accessed through p alone.
If p pointed to the front node, both operations would fail to be O(1). enQueue must change the rear node's next pointer, and deQueue must change the rear node's next pointer to the new front. The rear can only be reached by walking the whole list, which is O(n). Option (D), the node next to front, has the same problem: the rear is not reachable in O(1).
Answer: (A)
Consider the function f defined below.
struct item
{
int data;
struct item * next;
};
int f(struct item *p)
{
return ((p == NULL) || (p->next == NULL) ||
((p->data <= p->next->data) &&
f(p->next)));
} For a given linked list p, the function f returns 1 if and only if Answer: (B)
The function returns the value of this expression, which is an OR of three parts:
1. : the empty list gives 1.
2. : a list with one node gives 1.
3. AND : the first node is not bigger than the second, and the same check holds for the rest of the list.
So the function walks down the list and compares every node with the next one. It returns 1 only if every pair satisfies until the list ends. If any pair is out of order, the AND is false and the result is 0. This means the list is sorted in non-decreasing order, which is option (B).
Why the others are wrong:
(A) Empty and one-node lists do return 1, but so does a longer sorted list such as . So (A) is not "if and only if".
(C) For , the check fails, so the result is 0, though the list is non-increasing.
(D) For , the function returns 1, but all the values are the same.
Answer: (B)
1. : the empty list gives 1.
2. : a list with one node gives 1.
3. AND : the first node is not bigger than the second, and the same check holds for the rest of the list.
So the function walks down the list and compares every node with the next one. It returns 1 only if every pair satisfies until the list ends. If any pair is out of order, the AND is false and the result is 0. This means the list is sorted in non-decreasing order, which is option (B).
Why the others are wrong:
(A) Empty and one-node lists do return 1, but so does a longer sorted list such as . So (A) is not "if and only if".
(C) For , the check fails, so the result is 0, though the list is non-increasing.
(D) For , the function returns 1, but all the values are the same.
Answer: (B)
In the worst case, the number of comparisons needed to search a single linked list of length n for a given element is
Answer: (D)
A & C are not correct. We cannot do binary search efficiently {it takes O(n)} in a linked list.
B looks like the average case, but the question asks for the worst case.
The worst case is when the element is not in the list. Then we may search the whole list and compare with every element. So the answer is (D).
Keep practising