Skip to content
Official PYQ · 2020

GATE CS 2020 Q16 — Linked Lists

Explain with AI

What is the worst case time complexity of inserting nn elements into an empty linked list, if the linked list needs to be maintained in sorted order?

Choose one option
View worked solution

Answer: C

To find the worst-case time complexity of inserting nn elements into an initially empty sorted linked list, let us analyze the insertion of each element one by one:

  • 1st element: Inserted into an empty list. This takes Θ(1)\Theta(1) time.
  • 2nd element: Inserted into a list of size 1. In the worst case, we compare it with the existing element. This takes Θ(1)\Theta(1) time.
  • 3rd element: Inserted into a sorted list of size 2. In the worst case, we may need to traverse to the end of the list. This takes Θ(2)\Theta(2) operations.
  • ii-th element: Inserted into a sorted list of size i−1i-1. In the worst case, we must traverse the entire list of size i−1i-1 to find the correct insertion position. This takes Θ(i)\Theta(i) operations.

Summing the worst-case time complexities for inserting all nn elements:

T(n)=∑i=1nΘ(i)=Θ(1+2+3+⋯+n)=Θ(n(n+1)2)=Θ(n2)T(n) = \sum_{i=1}^{n} \Theta(i) = \Theta\left(1 + 2 + 3 + \dots + n\right) = \Theta\left(\frac{n(n+1)}{2}\right) = \Theta(n^2)

Thus, the worst-case time complexity is Θ(n2)\Theta(n^2).

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

Published
Report a correction
Keep exploringMore Linked Lists questions