简体   繁体   中英

Java sorted double-linked list: How to insert a new node quickly in the right position?

I have a double-linked list that needs to have fast inserts and removes. I can just transverse the whole thing in either direction to find where to insert or remove, but is there a more clever way to find the insertion or removal point ? First thing that came to mind was binary search, but since it is a linked list with no indexes (not an array), I am not sure how to jump around my linked list.

What is the right approach here to make insertions and removals the fastest they can be?

The smart way is to move towards a Skip List .

Other approaches include cacheing recent accesses and making intelligent guesses where to start your search, at the end, at the start or at a recent spot.

You may use a B-tree instead of the double linked list or, as mentioned in an other answer, use a Skip_list . For fast searching a double linked list is the wrong data-structure.

正如您自己指出的那样,因为链表不可索引,所以没有办法循环遍历列表以找到正确的插入点...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM