In a linked list, what is the time complexity of accessing an element by its index?

In a linked list, what is the time complexity of accessing an element by its index?

a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)

Answer:

b) O(n)

Explanation:

Accessing an element by its index in a linked list takes O(n) time because linked lists do not provide direct access to elements. To access the i-th element, you need to traverse the list from the head node, which requires linear time.

This is in contrast to arrays, which allow O(1) access to elements by their index. However, linked lists are more efficient than arrays for insertions and deletions, especially at the beginning or middle of the list.

Linked lists are often used when dynamic memory allocation and frequent insertions and deletions are required.

Reference:

DSA (Data Structures and Algorithms) MCQ Questions and Answers

Scroll to Top