Iterator Time Complexity for LinkedList in Java?

The code below will call an iterator and send the integer back to the method from which it was called. I just wanted to know if my temporal complexity with the help can Iteratorbecome permanent? (i.e., O (1)). Because if I use get with LinkedList, it will give me linear time (e.g. O (n)).

protected int least(int i) {
    if(i >= digits.size()) {
        return 0;           
    }

    // temp++;
    // System.out.println("Time taken=" + temp);
    // long start = System.nanoTime();

    ListIterator<Integer> itr1 = digits.listIterator(digits.size() - i);

    // System.out.println("Time elapsed:" + (System.nanoTime() - start));

    return itr1.previous();
}
+4
source share
3 answers

An iterator starting with the ith element

If you create Iteratorone that starts directly with the inth index LinkedList, you need to know that it also takes O(n). Finding an item in is LinkedListalways slow.

LinkedList head ( tail) . , .

(Javas LinkedList ):

LinkedList Structure

, Iterator, i -th, head ( tail) i -th. :

list.get(i);

, , O(n).


: ArrayList

, , ArrayList. O(1) ( start + i * sizeof(type)).

, , RandomAccess ( ) .


a LinkedList , , list.get(i). Iterator ( ListIterator, ).

Iterator:

Iterator<E> iter = list.iterator();
while (iter.hasNext()) {
    E element = iter.next();
    ...
}

, , :

for (E element : list) {
    ...
}

Javas LinkedList , . LinkedList#descendingIterator () LinkedList#iterator.

, , , ListIterator :

ListIterator<E> listIter = list.listIterator(0);
while (listIter.hasNext()) {
    E element = listIter.next();
    ...
    // Remove the element last polled
    listIter.remove();
    // Inserts an element right before the last polled element
    listIter.add(new Element());
}

ListIterator hasPrevious() previous(). .

+2

.

O(1). - O(n).

, listIterator(int):

public ListIterator<E> listIterator(int index) {
    checkPositionIndex(index);
    return new ListItr(index);
}

ListItr, , :

    ListItr(int index) {
        // assert isPositionIndex(index);
        next = (index == size) ? null : node(index);
        nextIndex = index;
    }

node, :

Node<E> node(int index) {
    // assert isElementIndex(index);

    if (index < (size >> 1)) {
        Node<E> x = first;
        for (int i = 0; i < index; i++) <<<<< HERE!
            x = x.next;
        return x;
    } else {
        Node<E> x = last;
        for (int i = size - 1; i > index; i--) <<<<<< HERE!
            x = x.prev;
        return x;
    }
}

? for , index. , O(n)! index.

+2

", ", , LinkedList , , , .

This means that a List.descendingIterator()is likely to be the best starting point if you need to start at the end, but you will always be in O (n) time to index into the structure LinkedList.

If you regularly dereference by index, you should usually use a random access structure such as ArrayList.

If you actually iterate over the list in any direction, then you should return your iterator, not the value.

+2
source

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


All Articles