fre*_*oma 0 big-o linked-list pseudocode
假设您有一个大小为N的链接列表,并且您想要从最后开始对每个元素执行操作.
我想出了以下伪代码:
while N > 0
Current = LinkedList
for 0 to N
Current = Current.tail
end
Operation(Current.head)
N := N-1
end
Run Code Online (Sandbox Code Playgroud)
现在我必须确定这个算法是哪个Big-O.
假设Operation()是O(1),我认为它是这样的:
N + (N-1) + (N-2) + ... + (N-(N-1)) + 1
Run Code Online (Sandbox Code Playgroud)
但我不确定Big-O实际上是什么.我认为它肯定小于O(N ^ 2),但我认为你不能说它的O(N)......