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 11–15 of 20
An unordered list contains n distinct elements. The number of comparisons to find an element in this list that is neither maximum nor minimum is
Answer: (D)
Pick any 3 elements from the list and compare them with each other. This takes 3 comparisons. The element in the middle of the three is not the maximum and not the minimum of the whole list.
The number of comparisons stays fixed, so it is constant.
Answer:
The number of comparisons stays fixed, so it is constant.
Answer:
The following C function takes a simply-linked list as input argument. It modifies the list by moving the last element to the front of the list and returns the modified list. Some part of the code is left blank.
typedef struct node {
int value;
struct node *next;
} Node;
Node *move_to_front(Node *head) {
Node *p, *q;
if ((head == NULL) || (head->next == NULL)) return head;
q = NULL; p = head;
while (p-> next !=NULL) {
q=p;
p=p->next;
}
_______________________________
return head;
} Choose the correct alternative to replace the blank line.Answer: (D)
When the loop ends, p points to the last node and q points to the second-last node. To move the last node to the front:
- : q becomes the new last node.
- : p links to the old first node.
- : p becomes the first node.
This is option (D).
Why the others fail:
- (A) only changes the local pointer q. The old second-last node still points to p, and closes a cycle.
- (B) runs before , so p's next becomes p itself, and the rest of the list is lost.
- (C) makes the list and then ends it, so all nodes before q are lost (for a list longer than 2 nodes).
Answer: (D)
- : q becomes the new last node.
- : p links to the old first node.
- : p becomes the first node.
This is option (D).
Why the others fail:
- (A) only changes the local pointer q. The old second-last node still points to p, and closes a cycle.
- (B) runs before , so p's next becomes p itself, and the rest of the list is lost.
- (C) makes the list and then ends it, so all nodes before q are lost (for a list longer than 2 nodes).
Answer: (D)
The following C function takes a single-linked list of integers as a parameter and rearranges the elements of the list. The function is called with the list containing the integers 1,2,3,4,5,6,7 in the given order. What will be the contents of the list after the function completes execution?
struct node {
int value;
struct node *next;
};
void rearrange(struct node *list) {
struct node *p, * q;
int temp;
if(!list|| !list-> next) return;
p=list; q=list->next;
while(q)
{
temp = p->value;
p->value = q->value;
q->value = temp;
p = q->next;
q = p?p->next:0;
}
} Answer: (B)
In each pass, and are two neighbouring nodes ( is just after ). Their values are swapped. Then and (or 0 if is NULL), so the next pair starts after the pair just swapped. The list is .
Pass 1: swap the values at nodes 1 and 2: .
Pass 2: swap nodes 3 and 4: .
Pass 3: swap nodes 5 and 6: .
After pass 3, points to node 7, which is not NULL, so . Now is NULL and the loop stops. Node 7 is never swapped, since it has no partner.
Answer: (B)
Pass 1: swap the values at nodes 1 and 2: .
Pass 2: swap nodes 3 and 4: .
Pass 3: swap nodes 5 and 6: .
After pass 3, points to node 7, which is not NULL, so . Now is NULL and the loop stops. Node 7 is never swapped, since it has no partner.
Answer: (B)
The following C function takes a singly-linked list of integers as a parameter and rearranges the elements of the list. The list is represented as pointer to a structure. The function is called with the list containing the integers 1,2,3,4,5,6,7 in the given order. What will be the contents of the list after the function completes execution?
struct node {int value; struct node *next;};
void rearrange (struct node *list) {
struct node *p, *q;
int temp;
if (!list || !list -> next) return;
p = list; q = list -> next;
while (q) {
temp = p -> value;
p -> value = q -> value;
q -> value = temp;
p = q -> next;
q = p ? p -> next : 0;
}
} Answer: (B)
Each pass of the loop swaps the values of the nodes p and q, where . Then p jumps two nodes ahead () and q becomes the node after it (). So the values are swapped in adjacent pairs.
Start: 1, 2, 3, 4, 5, 6, 7
Pass 1: swap nodes 1 and 2: 2, 1, 3, 4, 5, 6, 7
Pass 2: p at the 3rd node, q at the 4th: 2, 1, 4, 3, 5, 6, 7
Pass 3: p at the 5th node, q at the 6th: 2, 1, 4, 3, 6, 5, 7
Then p is the 7th node and , so the loop stops. The last value 7 has no partner, so it stays.
Result: 2, 1, 4, 3, 6, 5, 7
Answer: (B)
Start: 1, 2, 3, 4, 5, 6, 7
Pass 1: swap nodes 1 and 2: 2, 1, 3, 4, 5, 6, 7
Pass 2: p at the 3rd node, q at the 4th: 2, 1, 4, 3, 5, 6, 7
Pass 3: p at the 5th node, q at the 6th: 2, 1, 4, 3, 6, 5, 7
Then p is the 7th node and , so the loop stops. The last value 7 has no partner, so it stays.
Result: 2, 1, 4, 3, 6, 5, 7
Answer: (B)
Suppose there are sorted lists of elements each. The time complexity of producing a sorted list of all these elements is: (Hint: Use a heap data structure)
Answer: (A)
There are sorted lists. So we can build a min-heap of elements. To do this, we take the first element of each of the sorted lists. Next, we remove the minimum element from the heap. Then we add to the heap the next element from the sorted list that the removed element came from. (To know the source list, we can store a structure with two values in the heap: the number and the list it came from.) So each delete and its matching insert takes time. A delete on a heap of size costs , and an insert on a heap of size also costs . (Here the heap size is .) In all, there are elements. So the total time is .
Correct Answer:
Correct Answer:
Keep practising