从O(1)中删除双重链接列表

编程入门 行业动态 更新时间:2024-10-11 21:29:30
本文介绍了从O(1)中删除双重链接列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我知道这是从双重链接列表中删除元素的时间复杂度是O(1),它似乎很明显,但是如果我们没有收到一个元素来删除它,而是由元素包裹的那个,会发生什么例如,如果我们定义一个类元素来包装一个String(为了提供指向下一个和上一个元素的指针),我们可以在O(1)中执行删除)如果方法接收到该元素作为输入而不是字符串!

如果remove方法接收到字符串,则必须通过列表搜索找到相应的元素,右?所以这种情况下的时间复杂度将是O(n),而不是O(1)

class Element {字符串内容; 元素下一个; 元素上一个; } class LinkedList { ... public remove(String s){ //它必须首先找到与此对应的元素字符串! } }

解决方案

你完全正确。

删除被认为是 O(1)(当你做删除(元素)),但通常与find操作(即 remove(String)或 remove(find (String))),它是 O(n)。

I know that's the time complexity to remove an element from doubly linkedlist is O(1) and it seems obvious and clear BUT what happens if we don't receive an element to remove it but the one which is wrapped by the element?

for example if we define a class Element to wrap a String (To provide pointer pointing to the next and previous element) , we can do the removal in O(1) if the method receives that element as input not the string !

if the remove method receives the string , it has to search through the list to find the corresponding element , right ? so the time complexity in this case would be O(n) , not O(1) anymore

class Element{ String content; Element next; Element previous; } class LinkedList{ ... public remove(String s){ //it has to first find the element corresponding to this String ! } }

解决方案

You are exactly right.

Remove is considered O(1) (when you do remove(Element)), but usually this goes together with a find operation (i.e. remove(String) or remove(find(String))), which is O(n).

更多推荐

从O(1)中删除双重链接列表

本文发布于:2023-11-29 22:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647840.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:链接   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!