Is time complexity for insertion/deletion in a doubly linked list of order O(n)?(顺序为 O(n) 的双向链表中插入/删除的时间复杂度是否为 O(n)?)
问题描述
要在 DLL(双向链表)中插入/删除具有特定值的节点,需要遍历整个列表以找到位置,因此这些操作应该是 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).
如果是这样,那么 STL 列表(很可能使用 DLL 实现)如何能够在恒定时间内提供这些操作?
If that's the case then how come STL list (most likely implemented using DLL) is able to provide these operations in constant time?
谢谢大家让我说清楚.
推荐答案
在已知位置插入和删除是 O(1).然而,找到那个位置是 O(n),除非它是列表的头部或尾部.
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.
这篇关于顺序为 O(n) 的双向链表中插入/删除的时间复杂度是否为 O(n)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:顺序为 O(n) 的双向链表中插入/删除的时间复杂度是否为 O(n)?
基础教程推荐
- 这个宏可以转换成函数吗? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
