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 nn 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?

Choose one option
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.

  1. Doubly-Linked List (DLLdel): Each node has pointers to both next and prev. To delete a node given a pointer pp, we update p→prev→next=p→nextp\rightarrow prev\rightarrow next = p\rightarrow next and p→next→prev=p→prevp\rightarrow next\rightarrow prev = p\rightarrow prev. This requires no traversal and takes Θ(1)\Theta(1) time in the worst case.
  2. Singly-Linked List (SLLdel): Each node only points to next. Although an internal node can be deleted in O(1)O(1) by copying the data of p→nextp\rightarrow next into pp and deleting p→nextp\rightarrow next, this trick fails when pp is the last node of the list. Deleting the last node requires finding its predecessor, which necessitates traversing from the head pointer, taking Θ(n)\Theta(n) time in the worst case.

Thus, SLLdel is Θ(n)\Theta(n) and DLLdel is Θ(1)\Theta(1).

Final answer

Option (D)

Common trap

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.