algorithm - 数据结构 : Big O time cost

标签 algorithm linked-list big-o time-complexity

1) 向初始为空的单向链表添加 n 个元素的时间成本 插入到列表的前面。

答案似乎是 O(n) 或 O(1) 之一。 我认为它是 O(1) 因为 将一个元素插入空列表只是 例如 Node element = 1;

不过我还是不太确定。

2) 在具有 n 个元素的链表中查找数据元素的最佳情况时间成本是多少。

答案似乎也是 O(1) 或 O(n)。 我认为它是 O(n) 因为它必须遍历列表才能找到元素。

最佳答案

The time cost to add n elements to an initially empty singly linked list by 
inserting at the front of the list.

它是 O(1) 每次插入,但是你有 n 个 - 所以 O(n) 总计。

the best-case time cost to find a data element in a linked list with n elements

它是O(1),因为在最好的情况下 - 搜索到的元素是第一个,所以不需要遍历列表,搜索完第一个元素(这是常数时间)- 你可以停止。

关于algorithm - 数据结构 : Big O time cost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13815890/

相关文章:

algorithm - 将两个单位区间数转换为另一个单位区间数的函数

c# - 如何使用 C# 计算整数的二进制表示中的尾随零

c++ - SingleLinkedList 的析构函数导致段错误

java - Java 中的 LinkedList 迭代

python - 在由重复项组成的无序数组中查找唯一元素

java - 存储对象的 2D 网格

algorithm - 无法理解嵌套循环的大 O

algorithm - 根据单个数字获取下一个整数

algorithm - 谷歌公交太理想化了。你会如何改变它?

c++ - 如何实现 'square list' 迭代器