A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/java-util-linkedlist-add-method-in-java/ below:

LinkedList add() Method in Java

LinkedList add() Method in Java

Last Updated : 11 Jul, 2025

In Java, the add() method of the LinkedList class is used to add an element to the list. By default, it adds the element to the end of the list, if the index is not specified.

Example: Here, we use the add() method to add a single element to the list.

Java
// Java program to add elements in LinkedList
import java.util.LinkedList;

class Geeks {
    public static void main(String[] args) {
      
        // Create a LinkedList
        LinkedList<Integer> l = new LinkedList<>();

        // Adding elements in 
        // the list
        l.add(100);
    
        System.out.println("" + l);
    }
}

Now there are two versions of LinkedList add() method i.e. one with the specified index and one without any specified index.

1. public boolean add(E e)

Appends the element "e" to the end of the list.

Syntax:

public boolean add( E e)

Example: Here, we use the add() method to add multiple elements to the LinkedList without specifying an index.

Java
// Add elements without specified index
import java.util.LinkedList;

class Geeks {
    public static void main(String[] args) {
      
        // Create a LinkedList
        LinkedList<Integer> l = new LinkedList<>();

        // Adding elements in 
        // the list
        l.add(100);
        l.add(200);
        l.add(300);
        l.add(400);
        l.add(500);
       
        System.out.println("The LinkedList is: " + l);
    }
}

Output
The LinkedList is: [100, 200, 300, 400, 500]
2. public boolean add(index , element)

Appends the element at the specified index in the LinkedList.

Syntax:

list.add(index , element)

Parameters:

Example: Here, we use the add() method to add elements to the list with the specified index.

Java
// Add elements with specified index
import java.util.LinkedList;

class Geeks{
    public static void main(String[] args) {

        // Create a linkedlist
        LinkedList<Integer>l = new LinkedList<>();

        // Adding elements in 
        // the list
        l.add(100);
        l.add(200);
        l.add(400);
        System.out.println("LinkedList: " + l);

        l.add(2, 300);

        System.out.println("New LinkedList: " + l);
    }
}

Output
LinkedList: [100, 200, 400]
New LinkedList: [100, 200, 300, 400]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4