Skip to content
Official PYQ · 2016

GATE CS 2016 Set 2 Q15 — Linked Lists

Explain with AI

NN items are stored in a sorted doubly linked list. For a delete operation, a pointer is provided to the record to be deleted. For a decrease-key operation, a pointer is provided to the record on which the operation is to be performed.

An algorithm performs the following operations on the list in this order: Θ(N)\Theta \left(N\right) delete, O(log⁡N)O (\log N) insert, O(log⁡N)O(\log N) find, and Θ(N)\Theta(N) decrease-key. What is the time complexity of all these operations put together?

Choose one option
View worked solution

Answer: C

To find the overall time complexity, let us analyze the worst-case time complexity of each operation on a sorted doubly linked list of size NN:

  1. Delete Operation:

    We are given a direct pointer to the node to be deleted. In a doubly linked list, deleting a node when its pointer is known takes O(1)O(1) time because we can update its neighbors' pointers directly.

    Total time for Θ(N) deletions=Θ(N)×O(1)=Θ(N)\text{Total time for } \Theta(N) \text{ deletions} = \Theta(N) \times O(1) = \Theta(N)
  2. Insert Operation:

    To insert an element into a sorted doubly linked list, we must find its correct position by traversing the list sequentially. Since binary search is not possible on a linked list (due to lack of random access), finding the position takes O(N)O(N) time.

    Total time for O(log⁡N) insertions=O(log⁡N)×O(N)=O(Nlog⁡N)\text{Total time for } O(\log N) \text{ insertions} = O(\log N) \times O(N) = O(N \log N)
  3. Find Operation:

    Searching for an element in a linked list requires sequential traversal, which takes O(N)O(N) time.

    Total time for O(log⁡N) find operations=O(log⁡N)×O(N)=O(Nlog⁡N)\text{Total time for } O(\log N) \text{ find operations} = O(\log N) \times O(N) = O(N \log N)
  4. Decrease-Key Operation:

    We are given a pointer to the node. After decreasing its key, the list may no longer remain sorted. To maintain the sorted order, we must move the node to its correct position, which requires traversing the list backwards. In the worst case, this takes O(N)O(N) time.

    Total time for Θ(N) decrease-key operations=Θ(N)×O(N)=Θ(N2)\text{Total time for } \Theta(N) \text{ decrease-key operations} = \Theta(N) \times O(N) = \Theta(N^2)

Overall Time Complexity:

Summing the time complexities of all the operations performed in sequence:

Total Time=Θ(N)+O(Nlog⁡N)+O(Nlog⁡N)+Θ(N2)=O(N2)\text{Total Time} = \Theta(N) + O(N \log N) + O(N \log N) + \Theta(N^2) = O(N^2)

Thus, the overall time complexity is O(N2)O(N^2).

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

Published
Report a correction
Keep exploringMore Linked Lists questions