Let 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 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 denote the number of nodes in each of the linked lists. Which one of the following choices is about the worst-case time complexity of and
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…
- Ais and is
- BBoth and are
- CBoth and are
- Dis and isCorrect 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.