Question
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?
Official answer and solution
- Official answer
- C
- From
- Official GATE 2020 answer key
Solution
Analyze the number of node comparisons required to insert n elements sequentially into a sorted linked list.
A singly linked list does not support random access; traversing to the correct insertion position requires linear scanning from the head of the list.
In the worst case (for example, inserting elements in ascending order when scanning from the head, or descending order depending on list orientation), inserting the -th element into a list that already contains elements requires traversing all existing nodes.
Summing the comparisons over all insertions:
Therefore, the worst-case time complexity is .
C
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2020 Computer Science and Information Technology (CS)
- Question
- 26 of 65, printed as CS Q.16
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.