LinkedList addLast() Method in Java
Last Updated :
18 Dec, 2024
Improve
In Java, the addLast() method of the LinkedList class is used to add an element at the end of the list.
Syntax of LinkedList addLast() Method
void addLast( E e)
- Parameter: e is the element you want to add at the end of the list.
- Return type: This method does not return any value.
Example: Here, we use the addLast() method to add elements at the end of the LinkedList.
// Java program to demonstrate the
// use of addLast() method in a LinkedList
import java.util.LinkedList;
public class Geeks{
public static void main(String args[]) {
// Create an empty list
LinkedList<String> l = new LinkedList<>();
l.add("Geeks");
l.add("for");
l.add("Geeks");
System.out.println("The LinkedList is: " + l);
// Adding new elements at the
// end of the list using addLast()
l.addLast("is");
l.addLast("the");
l.addLast("best");
l.addLast("Coding");
l.addLast("Platform");
System.out.println("The new LinkedList is: " + l);
}
}
Output
The LinkedList is: [Geeks, for, Geeks] The new LinkedList is: [Geeks, for, Geeks, is, the, best, Coding, Platform]