What is the time complexity of LinkedList.getLast() in Java?

后端 未结 5 692
伪装坚强ぢ
伪装坚强ぢ 2020-12-17 15:06

I have a private LinkedList in a Java class & will frequently need to retrieve the last element in the list. The lists need to scale, so I\'m trying to decide whether I

5条回答
  •  佛祖请我去吃肉
    2020-12-17 15:47

    It is O(1) because the list is doubly-linked. It keeps references to both head and tail.

    From documentation:

    Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.

提交回复
热议问题