Do elements in a LinkedList java class have an index?

Do elements in a LinkedList java class have an index? If so , why is its worst case performance o (n) for the search operation, when it can directly search using the object index? If not , how can we insert an object in the middle of a linked list using a method void add(int index, Object item)?

+4
source share
2 answers

They have a logical index, yes - effectively the number of times you need to iterate, starting from the head, before moving on to this node.

This is not the same as saying “it can directly search using the index of an object” - because it cannot.

O (1) , - . node N, N ..., O (N).

+6

LinkedList , .

i 'th ( add(int index, Object item)), LinkedList ( , ) , .

+3

Source: https://habr.com/ru/post/1598559/


All Articles