This example shows how to add an element at the beginning of the LinkedList (front) in Java. The example also shows how to insert an element at any index of the LinkedList.
How to add an element at the beginning of the LinkedList (at the front)?
1. Using the add method
When you add an element to the LinkedList using the add
method, by default it is appended at the end of the LinkedList object. What if you want to add an element at the beginning of the LinkedList or at the front of the LinkedList?
Well, in that case, you can use the overloaded add
method that also accepts the index argument.
1 |
public void add(int index, E element) |
This add
method adds the specified element at the specified index in the LinkedList object. It also shifts any element located at the current position and all other subsequent elements to the right by increasing their indices by 1.
In order to add the element at the beginning, we need to call this method with index 0 as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.LinkedList; public class LinkedListAddToFrontExample { public static void main(String[] args) { LinkedList<String> linkedListColors = new LinkedList<String>(); linkedListColors.add("Red"); linkedListColors.add("Green"); linkedListColors.add("Blue"); /* * To add an element at the front of the LinkedList, use * the overloaded add method and specify the index as 0 */ linkedListColors.add(0, "Yellow"); System.out.println( "LinkedList contains: " + linkedListColors ); } } |
Output
1 |
[Yellow, Red, Green, Blue] |
As you can see from the output, the element “Yellow” is inserted at the front i.e. index 0 and all the current subsequent element’s indices are increased by 1 thus shifting them to the right.
2. Using the addFirst method
You can also use the addFirst
method declared in the Deque interface to add an element at the front of the LinkedList object.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
LinkedList<String> linkedListColors = new LinkedList<String>(); linkedListColors.add("Red"); linkedListColors.add("Green"); linkedListColors.add("Blue"); /* * The addFirst method also inserts an element * at the beginning of the LinkedList object */ linkedListColors.addFirst("Black"); System.out.println( "LinkedList contains: " + linkedListColors ); |
Output
1 |
LinkedList contains: [Black, Red, Green, Blue] |
How to insert an element at the specified index in the LinkedList?
Specify the index at which you want to insert an element in the LinkedList in the add
method. All the existing elements will be shifted to the right by the add
method automatically.
The below given example inserts the “Black” element at index 1 of the LinkedList object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.LinkedList; public class LinkedListAddToFrontExample { public static void main(String[] args) { LinkedList<String> linkedListColors = new LinkedList<String>(); linkedListColors.add("Red"); linkedListColors.add("Green"); linkedListColors.add("Blue"); /* * To insert an element at any index in the LinkedList, use * the add method and specify that index */ linkedListColors.add(1, "Black"); System.out.println( "LinkedList contains: " + linkedListColors ); } } |
Output
1 |
LinkedList contains: [Red, Black, Green, Blue] |
As you can see from the output, the “Black” element is inserted at index 1 and the index of the “Green” and “Blue” elements have been increased to 2 and 3 respectively.
This example is a part of the Java LinkedList tutorial with examples.
Please let me know your views in the comments section below.