- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add elements at beginning and end of LinkedList in Java
Elements can be added at the beginning of a LinkedList by using the method java.util.LinkedList.addFirst(). This method has a single argument i.e. the element that is to be added at the beginning of the LinkedList.
Elements can be added at the end of a LinkedList by using the method java.util.LinkedList.addLast(). This method has a single argument i.e. the element that is to be added at the end of the LinkedList.
A program that demonstrates this is given as follows −
Example
import java.util.LinkedList; public class Demo { public static void main(String[] args) { LinkedList<String> l = new LinkedList<String>(); l.add("Apple"); l.add("Mango"); l.add("Pear"); System.out.println("The LinkedList is: " + l); l.addFirst("Orange"); l.addLast("Guava"); System.out.println("The LinkedList is: " + l); } }
Output
The LinkedList is: [Apple, Mango, Pear] The LinkedList is: [Orange, Apple, Mango, Pear, Guava]
Now let us understand the above program.
The LinkedList l is created. Then LinkedList.add() is used to add the elements to the LinkedList. Then the LinkedList is displayed. A code snippet which demonstrates this is as follows −
LinkedList<String> l = new LinkedList<String>(); l.add("Apple"); l.add("Mango"); l.add("Pear"); System.out.println("The LinkedList is: " + l);
The methods LinkedList.addFirst() and LinkedList.addLast() is used to insert the elements “Orange” and “Guava” at the beginning and end of the Linked List respectively. Then the LinkedList is again displayed. A code snippet which demonstrates this is as follows −
l.addFirst("Orange"); l.addLast("Guava"); System.out.println("The LinkedList is: " + l);
- Related Articles
- Add elements at the end of a Vector in Java
- How to Add Text at the Beginning or End of All Cells in Excel?
- Java Program to Add elements to a LinkedList
- How to add new array elements at the beginning of an array in JavaScript?
- Adding new node or value at the end of LinkedList in C#
- Java Program to get the beginning and end date of the week
- Beginning and end pairs in array - JavaScript
- Get first and last elements from Java LinkedList
- Add elements at the middle of a Vector in Java
- Java Program to remove whitespace from the beginning and end of a string
- Remove a range of elements from a LinkedList in Java
- Create an object array from elements of LinkedList in Java
- PHP program to add item at the beginning of associative array
- Add a single element to a LinkedList in Java
- Iterate through elements of a LinkedList using a ListIterator in Java
