YouTip LogoYouTip

Java Linkedlist Peeklast

# Java LinkedList peekLast() Method [![Image 3: Java LinkedList](#) Java LinkedList](#) * * * `peekLast()` is a method provided by the `LinkedList` class in Java, used to view but not remove the last element of the list. If the list is empty, this method returns `null`. ### Syntax Format E peekLast() * * * ## Method Characteristics ### 1. Non-destructive Operation The `peekLast()` method only views the last element of the list without removing it. ### 2. Empty List Handling When the list is empty, `peekLast()` returns `null` instead of throwing an exception. ### 3. Time Complexity Since `LinkedList` is implemented as a doubly linked list, the time complexity of the `peekLast()` method is O(1). * * * ## Method Parameters and Return Value ### Parameters This method does not accept any parameters. ### Return Value * Returns the last element of the list * Returns `null` if the list is empty * * * ## Usage Examples ### Basic Usage ## Example import java.util.LinkedList; public class PeekLastExample { public static void main(String[] args){ LinkedList list =new LinkedList(); list.add("Apple"); list.add("Banana"); list.add("Cherry"); String lastElement = list.peekLast(); System.out.println("Last element: "+ lastElement);// Output: Cherry System.out.prin
← Java Linkedlist CloneJava Linkedlist Element β†’