Open In App

Time and Space Complexity of Linked List

The time complexity of the Linked List is O(n) for Accessing, insertion, or deletion at the beginning and searching for an element, where n is the number of elements. However, insertion and deletion at the beginning or end take O(1) time in a doubly linked list due to direct access to the head and tail nodes.

Space complexity for both types of linked lists is O(n), as each element requires space for its data and pointers to the next (and possibly previous) node, resulting in linear space usage proportional to the number of elements.



Here’s a table summarizing the time and space complexity of common operations for a linked list:



Operation Time Complexity (Singly Linked List) Time Complexity (Doubly Linked List) Space Complexity
Accessing by Index O(n) O(n) O(1)
Insertion at Beginning O(1) O(1) O(1)
Insertion at End O(n) O(1) O(1)
Insertion at Given Position O(n) O(n) O(1)
Deletion at Beginning O(1) O(1) O(1)
Deletion at End O(n) O(1) O(1)
Deletion at Given Position O(n) O(n) O(1)
Searching O(n) O(n) O(1)

Time Complexity of Searching (Finding an Element) in Linked List:

Time Complexity of Insertion (Adding an Element) in Linked List:

Time Complexity of Deletion (Removing an Element) in Linked List:

Time Complexity of Traversal in Linked List: O(n)

Time Complexity of Accessing (Getting/Setting) an element in Linked List: O(n)

Auxiliary Space Complexity of Linked List:

The auxiliary space Complexity of above linked list operations mentioned above is O(1) because they do not require extra space beyond a fixed number of variables. However, for certain operations, extra space of the order of O(N) may be needed. For instance, sorting a linked list using a non-in-place sorting algorithm would require this additional space.

Article Tags :