In Java, why is insertion or deletion in a Linked List a constant time operation? Isn't it misleading?

前端 未结 4 1207
盖世英雄少女心
盖世英雄少女心 2021-01-04 09:43

Insertion or deletion of an element at a specific point of a list, assuming that we have a pointer to the node already, is a constant-time operation. - from

4条回答
  •  死守一世寂寞
    2021-01-04 10:34

    You are missing the point I think here. It is just the INSERTION and DELETION that have a constant time not the finding the point of insertion or deletion as well! The time is constant because you simply need to set the references (links) to the previous and next item in the list -- whereas for instance with ArrayList, in the case of insertion you need to allocate memory for (at least) one more item and transfer the existing data into the newly allocated array (or with deletion you have to shift elements in the array around once you deleted the item).

提交回复
热议问题