The time complexity of various operations on linked lists can vary depending on the type of linked list (singly, doubly, or circular) and the specific operation being performed. Here is a detailed breakdown of the time complexities for common operations on linked lists:
Singly Linked List
Insertion
- At the beginning: $$O(1)$$ - This operation involves updating the head pointer to the new node, which is a constant-time operation[2][3][7].
- At the end: $$O(n)$$ - This requires traversing the entire list to find the last node, making it a linear-time operation[3][7].
- At a given position: $$O(n)$$ - This involves traversing the list to the desired position, which takes linear time[3][7].
Deletion
- At the beginning: $$O(1)$$ - This operation involves updating the head pointer to the next node, which is a constant-time operation[2][3][7].
- At the end: $$O(n)$$ - This requires traversing the entire list to find the second-to-last node, making it a linear-time operation[3][7].
- At a given position: $$O(n)$$ - This involves traversing the list to the desired position, which takes linear time[3][7].
Access/Search
- Access by index: $$O(n)$$ - Accessing an element by index requires traversing the list from the head to the desired position, which takes linear time[3][7].
- Search for an element: $$O(n)$$ - Searching for an element involves traversing the list until the element is found, which takes linear time[3][7].
Traversal
- Traversal: $$O(n)$$ - Traversing the entire list to visit each node takes linear time[3][7].
Doubly Linked List
Insertion
- At the beginning: $$O(1)$$ - This operation involves updating the head pointer and the previous pointer of the old head node, which is a constant-time operation[2][3][7].
- At the end: $$O(1)$$ - This operation involves updating the tail pointer and the next pointer of the old tail node, which is a constant-time operation[2][3][7].
- At a given position: $$O(n)$$ - This involves traversing the list to the desired position, which takes linear time[3][7].
Deletion
- At the beginning: $$O(1)$$ - This operation involves updating the head pointer and the previous pointer of the new head node, which is a constant-time operation[2][3][7].
- At the end: $$O(1)$$ - This operation involves updating the tail pointer and the next pointer of the new tail node, which is a constant-time operation[2][3][7].
- At a given position: $$O(n)$$ - This involves traversing the list to the desired position, which takes linear time[3][7].
Access/Search
- Access by index: $$O(n)$$ - Accessing an element by index requires traversing the list from the head or tail to the desired position, which takes linear time[3][7].
- Search for an element: $$O(n)$$ - Searching for an element involves traversing the list until the element is found, which takes linear time[3][7].
Traversal
- Traversal: $$O(n)$$ - Traversing the entire list to visit each node takes linear time[3][7].
Circular Linked List
Insertion