Java Program to Get the Last Item From the LinkedList Without Removing It

Java program to get the last item from the LinkedList without removing it

// Java program to get the last item from the 
// LinkedList without removing it

import java.util.LinkedList;

public class Main {
  public static void main(String[] args) {
    LinkedList list = new LinkedList();

    list.add(1);
    list.add("TWO");
    list.add(3);
    list.add("FOUR");
    list.add(true);

    System.out.println("List Items: \n" + list);
    System.out.println("Last Item: " + list.peekLast());
  }
}

Output

List Items: 
[1, TWO, 3, FOUR, true]
Last Item: true
Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment