Java Program to Insert an Element at the Beginning of the LinkedList Collection

Java program to insert an element at the beginning of the LinkedList collection

// Java program to insert an element at the beginning 
// of the LinkedList collection

import java.util.LinkedList;

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

    countries.add("India");
    countries.add("USA");
    countries.add("UK");
    countries.add("CANADA");

    System.out.println("Country names: " + countries);

    countries.addFirst("JAPAN");

    System.out.println("Country names: " + countries);
  }
}

Output

Country names: [India, USA, UK, CANADA]
Country names: [JAPAN, India, USA, UK, CANADA]
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