Why is deleting in a single linked list O(1)?

后端 未结 8 2447
悲哀的现实
悲哀的现实 2020-12-25 14:50

I do not quiet understand why deleting at the end of a single linked list goes in O(1) time, as the wikipedia article says.

A single linked list consists out of node

8条回答
  •  一整个雨季
    2020-12-25 15:28

    The addition/deletion of ANY node at ANY location is O(1). Code just play with fixed cost (few pointers calculations and malloc/frees) to add/delete the node. This arithmetical cost is fixed for any specific case.

    However, the cost to reach(Indexing) the desired node is O(n).

    The article is merely listing the addition/deletion in multiple sub-categories(adding in middle, beginning, end) to show that cost for adding in middle differs than adding in beginning/end (But the respective costs are still fixed).

提交回复
热议问题