Skip to main content

GATE CSE 2023Data Structures › Stacks, Queues, Linked Lists & ArraysVerified

GATE CSE 2023 PYQ: Let SLLdel \textsf{SLLdel} SLLdel be a function that deletes a node in a singly-linked list given a pointer…

Let SLLdel\textsf{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\textsf{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 nn denote the number of nodes in each of the linked lists. Which one of the following choices is TRUE\text{TRUE} about the worst-case time complexity of SLLdel\textsf{SLLdel} and DLLdel?\textsf{DLLdel}?

  1. A
    SLLdel\textsf{SLLdel} is O(1)O(1) and DLLdel\textsf{DLLdel} is O(n)O(n)
  2. B
    Both SLLdel\textsf{SLLdel} and DLLdel\textsf{DLLdel} are O(log⁡(n))O(\log (n))
  3. C
    Both SLLdel\textsf{SLLdel} and DLLdel\textsf{DLLdel} are O(1)O(1)
  4. D
    SLLdel\textsf{SLLdel} is O(n)O(n) and DLLdel\textsf{DLLdel} is O(1)O(1)
    Correct answer

Answer

(D) SLLdel \textsf{SLLdel} SLLdel is O ( n ) O(n) O ( n ) and DLLdel \textsf{DLLdel} DLLdel is O ( 1 ) O(1) O ( 1 )

Approach

The key difference is that a singly linked node stores only a next pointer, while a doubly linked node stores both prev and next pointers. Deletion requires access to the predecessor, so the available pointers determine the worst-case cost.

In a singly linked list, the only way to find the node before the given node is to walk from the head, so worst-case deletion is O(n). In a doubly linked list, the previous node is immediately available through the node's prev pointer, so deletion is O(1). Hence option D is correct.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.