Fetching element from a LinkedList

Let’s discuss the different methods to fetch an element from LinkedList.

Fetching the first element

We can use the getFirst() method to fetch the first element in the list. If the LinkedList is empty, then NoSuchElementException is thrown.

Fetching the last element

We can use the getLast() method to fetch the last element in the list. If the LinkedList is empty, then NoSuchElementException is thrown.

Fetching an element at a particular index

We can fetch an element at a particular index by using the get(int index) method. The index should be more than zero and less than the size of the LinkedList; otherwise, IndexOutOfBoundsException is thrown.

Get hands-on with 1200+ tech skills courses.