Junior — Middle
What is the time complexity of deleting an element in a linked list?
sobes.tech AI
Answer from AI
The time complexity of deleting an element in a linked list depends on whether the node to be deleted is known:
- If there is a pointer to the node itself, deletion occurs in O(1), as it is enough to reconfigure the links of neighboring nodes.
- If you need to first find the element by value or position, then the search takes O(n), and the overall deletion complexity will be O(n).
Thus, without access to the node — O(n), with access — O(1).