Skip to content
Official PYQ · 2023

GATE CS 2023 Q3 — Linked Lists

Explain with AI

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

Choose one option
View worked solution

Answer: D

To determine the worst-case time complexity of deleting a node from a singly-linked list (SLLdel\textsf{SLLdel}) and a doubly-linked list (DLLdel\textsf{DLLdel}), let us analyze the steps required for each:

1. Singly-Linked List (SLLdel\textsf{SLLdel})

  • To delete a given node PP from a singly-linked list, we must update the next pointer of its predecessor node to point to P→nextP\to\text{next}.
  • Since a singly-linked list only has forward pointers, we cannot directly access the predecessor of PP.
  • We must traverse the list starting from the head to find the predecessor node QQ such that Q→next=PQ\to\text{next} = P.
  • In the worst case (for example, when PP is the last node in the list), this traversal requires visiting all nn nodes, which takes O(n)O(n) time.
  • Note: While a trick exists to copy the data of the next node into PP and delete the next node in O(1)O(1) time, this trick fails if PP is the last node of the list. Thus, the worst-case time complexity remains O(n)O(n).

2. Doubly-Linked List (DLLdel\textsf{DLLdel})

  • In a doubly-linked list, every node contains pointers to both its successor (next) and its predecessor (prev).
  • We can directly access the predecessor of PP using P→prevP\to\text{prev} and the successor using P→nextP\to\text{next}.
  • The deletion can be performed in constant time by updating the pointers:
    if (P->prev != NULL) P->prev->next = P->next;
    if (P->next != NULL) P->next->prev = P->prev;
  • Since no traversal is needed, the worst-case time complexity is O(1)O(1).

Thus, SLLdel\textsf{SLLdel} is O(n)O(n) and DLLdel\textsf{DLLdel} is O(1)O(1).

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Linked Lists questions