What is the worst case time complexity of inserting elements into an empty linked list, if the linked list needs to be maintained in sorted order?
View worked solution
Answer: C
To find the worst-case time complexity of inserting 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 time.
- 2nd element: Inserted into a list of size 1. In the worst case, we compare it with the existing element. This takes 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 operations.
- -th element: Inserted into a sorted list of size . In the worst case, we must traverse the entire list of size to find the correct insertion position. This takes operations.
Summing the worst-case time complexities for inserting all elements:
Thus, the worst-case time complexity is .
Free practice · Your answers on this page are not saved.
