Why deletion of elements of hash table using doubly-linked list is O(1)?

前端 未结 8 1356
野的像风
野的像风 2020-12-23 10:20

On CLRS\'s textbook \"Introduction to Algorithm\", there\'s such paragraph on pg. 258.

We can delete an element in O(1) time if the lists are doubly linked. (Note th

8条回答
  •  清酒与你
    2020-12-23 11:00

    Coding point of view: one can use unordered_map in c++ to implement this.

    unordered_mapmp;
    

    Where node* is a pointer to a structure storing the key, left and right pointers!

    How to use:

    If you have a value v and you want to delete that node just do:

    1. Access that nodes value like mp[v].

    2. Now just make its left pointer point to the node on its right.

    And voila, you are done.

    (Just to remind, in C++ unordered_map takes an average O(1) to access a specific value stored.)

提交回复
热议问题