What is the time complexity of accessing an element in an array and a linked list?

Arrays Linked Lists Questions Medium



46 Short 80 Medium 67 Long Answer Questions Question Index

What is the time complexity of accessing an element in an array and a linked list?

The time complexity of accessing an element in an array is O(1) or constant time. This is because arrays have a fixed size and elements are stored in contiguous memory locations. Therefore, accessing any element in an array can be done directly by calculating its memory address using the index.

On the other hand, the time complexity of accessing an element in a linked list is O(n) or linear time. This is because linked lists do not have a fixed size and elements are not stored in contiguous memory locations. To access a specific element in a linked list, we need to traverse through the list starting from the head node until we reach the desired element. The time taken to access an element in a linked list increases linearly with the size of the list.

In summary, accessing an element in an array has a constant time complexity of O(1), while accessing an element in a linked list has a linear time complexity of O(n).