Open In App

LinkedList getLast() Method in Java

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
6 Likes
Like
Report

In Java, the getLast() method in the LinkedList class is used to retrieve the last element of the list.

Example 1: Here, we use the getLast() method to retrieve the last element of the list.


Output
[Geeks, for, Geeks]
Geeks

Syntax of LinkedList getLast() Method

public E getLast()

Return type: It return the last element of the list.

LinkedList getLast() Method work flow


Example 2: Here, the getLast() method throws an NoSuchElementException if the list is empty.


Output
The last element of the LinkedList is: 50
Error: java.util.NoSuchElementException

Explore