Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is time complexity for insertion/deletion in a doubly linked list of order O(n)?

To insert/delete a node with a particular value in DLL (doubly linked list) entire list need to be traversed to find the location hence these operations should be O(n).

If that's the case then how come STL list (most likely implemented using DLL) is able to provide these operations in constant time?

Thanks everyone for making it clear to me.

like image 570
ajay Avatar asked Oct 10 '10 07:10

ajay


1 Answers

Insertion and deletion at a known position is O(1). However, finding that position is O(n), unless it is the head or tail of the list.

When we talk about insertion and deletion complexity, we generally assume we already know where that's going to occur.

like image 98
GManNickG Avatar answered Sep 23 '22 19:09

GManNickG