What is the time complexity of inserting an element into a sorted linked list?
a) O(1)
b) O(log n)
c) O(n)
d) O(n log n)
Answer:
c) O(n)
Explanation:
Inserting an element into a sorted linked list has a time complexity of O(n) because it requires traversing the list to find the correct insertion point. Once the correct position is found, the insertion itself takes constant time O(1).
This linear time traversal is necessary because linked lists do not provide direct access to their elements, unlike arrays, which allow access via index in constant time.
Sorted linked lists are used in applications where frequent insertions and deletions are needed while maintaining sorted order, such as in priority queues.
Reference:
DSA (Data Structures and Algorithms) MCQ Questions and Answers