Time and Space Complexity of Linked List
Last Updated :
31 Jul, 2024
Improve
A linked list is a fundamental data structure in computer science and programming. It is a collection of nodes where each node contains a data field and a reference (link) to the next node in the sequence. The last node in the list points to null, indicating the end of the list. Knowing the time and space complexity of linked lists is important for improving algorithms and applications that use them. In this article, we are going to take a look at the complexity analysis of common operations of linked lists.
Complexity Analysis of different Operations on Linked List:
Table of Content
- Complexity Analysis of Insertion at the Beginning of Linked List
- Complexity Analysis of Insertion at the End of Linked List
- Complexity Analysis of Insertion at a Specific Position of Linked List
- Complexity Analysis of Deletion at the Beginning of Linked List
- Complexity Analysis of Deletion at the End of Linked List
- Complexity Analysis of Deletion at a Specific Position of Linked List
- Complexity Analysis of Search for a Value of Linked List
Below table represents the time and space complexities for various operations on a linked list:
Operation | Time Complexity | Auxiliary Space | Explanation |
---|---|---|---|
Insertion at Beginning | O(1) | O(1) | Constant-time pointer updates. |
Insertion at End | O(n) | O(1) | Traversal required to find the last node. |
Insertion at Position | O(n) | O(1) | Traversal to the desired position, then constant-time pointer updates. |
Deletion at Beginning | O(1) | O(1) | Constant-time pointer update. |
Deletion at End | O(n) | O(1) | Traversal required to find the second last node. |
Deletion at Position | O(n) | O(1) | Traversal to the desired position, then constant-time pointer updates. |
Searching in Linked list | O(n) | O(1) | Traversal through the list to find the desired value. |
Let's look at time and auxiliary space complexity of each of these above operations in detail.
Complexity Analysis of Insertion at the Beginning of Linked List
- Time Complexity: O(1)
- Reason: Inserting a node at the beginning involves the following steps:
- Create a new node.
- Set the next pointer of new node to the current head.
- Update the head to point to the new node.
- Reason: Inserting a node at the beginning involves the following steps:
- Auxiliary Space: O(1)
- Reason: The space required for the operation is constant because only one new node is created, and no additional data structures or memory proportional to the input size are needed.
Complexity Analysis of Insertion at the End of Linked List
- Time Complexity: O(n)
- Reason: Inserting a node at the end requires:
- Traversing the entire list to find the last node (which takes O(n) time where n is the number of nodes).
- Setting the next pointer of the last node to the new node.
- Optionally, updating the last node to point to null if needed.
- Reason: Inserting a node at the end requires:
- Auxiliary Space: O(1)
- Reason: Only one new node is created, and no extra space proportional to the input size is needed.
Complexity Analysis of Insertion at a Specific Position of Linked List
- Time Complexity: O(n)
- Reason: Inserting at a specific position involves:
- Traversing the list to the node just before the desired position (which can take O(n) time in the worst case).
- Creating a new node.
- Setting the new node's next pointer to the next node in the list.
- Updating the previous node's next pointer to point to the new node.
- Reason: Inserting at a specific position involves:
- Auxiliary Space: O(1)
- Reason: Only one new node is created, and no additional memory proportional to the list size is required.
Complexity Analysis of Deletion at the Beginning of Linked List
- Time Complexity: O(1)
- Reason: Deleting the first node involves:
- Updating the head pointer to point to the next node.
- Reason: Deleting the first node involves:
- Auxiliary Space: O(1)
- Reason: No additional memory is required for this operation.
Complexity Analysis of Deletion at the End of Linked List
- Time Complexity: O(n)
- Reason: Deleting the last node requires:
- Traversing the entire list to find the second-to-last node (which takes O(n) time).
- Updating the second-to-last node's next pointer to null.
- Reason: Deleting the last node requires:
- Auxiliary Space: O(1)
- Reason: No additional memory is required for this operation.
Complexity Analysis of Deletion at a Specific Position of Linked List
- Time Complexity: O(n)
- Reason: Deleting a node at a specific position involves:
- Traversing the list to the node just before the one to be deleted (which can take O(n) time in the worst case).
- Updating the previous node's next pointer to bypass the node to be deleted and point to the next node.
- Reason: Deleting a node at a specific position involves:
- Auxiliary Space: O(1)
- Reason: No additional memory is required for this operation.
Complexity Analysis of Search for a Value of Linked List
- Time Complexity: O(n)
- Reason: Searching for a value involves:
- Traversing the list node by node until the desired value is found or the end of the list is reached.
- Reason: Searching for a value involves:
- Auxiliary Space: O(1)
- Reason: No additional memory is required for this operation.