Time Complexity in singly link list

前端 未结 3 2017
栀梦
栀梦 2021-02-14 14:13

I am studying data-structure: singly link list.

The website says singly linked list has a insertion and deletion time complexity of O(1). Am I missing some

3条回答
  •  鱼传尺愫
    2021-02-14 14:52

    You missed the interface at two places:

    1. std::list::insert()/std:list::erase() need an iterator to the element where to insert or erase. This means you have no search but only alter two pointers in elements in the list, which is constant complexity.

    2. Inserting at the end of a list can be done via push_back. The standard requires this to be also O(1). Which means, if you have a std::list, it will store first and last element.

    EDIT: Sorry, you meet std::forward_list. Point 1 holds also for this even if the names are insert_after and erase_after. Points 2 not, you have to iterate to the end of the list.

提交回复
热议问题