Question
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 a 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 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?
Official answer and solution
- Official answer
- D
- From
- Official GATE 2023 answer key
Solution
Analyze deletion in singly-linked versus doubly-linked lists when given only a pointer to the target node.
- Doubly-Linked List (DLLdel): Each node has pointers to both
nextandprev. To delete a node given a pointer , we update and . This requires no traversal and takes time in the worst case. - Singly-Linked List (SLLdel): Each node only points to
next. Although an internal node can be deleted in by copying the data of into and deleting , this trick fails when is the last node of the list. Deleting the last node requires finding its predecessor, which necessitates traversing from the head pointer, taking time in the worst case.
Thus, SLLdel is and DLLdel is .
Option (D)
Assuming the data-copy trick works for all nodes, overlooking the tail node boundary case in singly-linked lists.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2023 Computer Science and Information Technology (CS)
- Question
- 13 of 65
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.