LinkedList getLast() Method in Java
Linked List is a part of the Collection framework present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and. The Java.util.LinkedList.getLast() method is used to fetch or retrieve the last element from a LinkedList or the element present at the tail of the list.
Input : 4 --> 1 --> 5 --> 6 --> 1 --> 3
As we all know that getLast() is one of the methods been there up present inside LinkedList class. It retrieves the last element from the object inside which elements of LinkedList are present. As we know LinkedList is a class been present inside java.util package so and if we do operate the following methods the syntax will look like as shown below as follows:
LinkedList ll = new LinkedList<T>();
In simpler words, if we want to know the last element or the tail of LinkedList then do perform the below check as listed below. It will, later on, be illustrated as an example while implementing.
Return Type: The last element or the element present at the tail of the list.
LinkedList<String> list =
"The last element is: "
LinkedList:[Geeks, for, Geeks, 10, 20]
The last element is: 20
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!