How to Add Element at First and Last Position of LinkedList in Java?

Last Updated : 26 Aug, 2026

LinkedList in Java allows elements to be inserted at both the beginning and the end of the list. The addFirst() and addLast() methods provide a simple way to perform these operations.

  • addFirst() inserts an element at the beginning of the LinkedList.
  • addLast() inserts an element at the end of the LinkedList.
  • The add() method can also be used to add an element at the end of the list.

Example:

Input: LinkedList = [10, 20, 30]
Insert at first = 5
Insert at last = 40

Output: [5, 10, 20, 30, 40]

Methods to Add Elements at First and Last Position

There are several ways to add elements at the beginning and end of a LinkedList:

1. Using addFirst() and addLast()

The LinkedList class provides two methods specifically for adding elements at its ends.

addFirst():

The addFirst() method inserts the specified element at the beginning of the list.

Syntax of addFirst(): linkedList.addFirst(element);

addLast():

The addLast() method inserts the specified element at the end of the list.

Syntax of addLast(): linkedList.addLast(element);

Approach

  1. Create a LinkedList.
  2. Add some elements using add().
  3. Use addFirst() to insert an element at the beginning.
  4. Use addLast() to insert an element at the end.
  5. Print the updated list.
Java
import java.util.LinkedList;

public class AddFirstLast{

    public static void main(String[] args)
    {

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

        // Add elements to the list
        list.add(10);
        list.add(20);
        list.add(30);

        System.out.println("Original LinkedList: " + list);

        // Add element at the first position
        list.addFirst(5);

        // Add element at the last position
        list.addLast(40);

        System.out.println("Updated LinkedList: " + list);
    }
}

Output
Original LinkedList: [10, 20, 30]
Updated LinkedList: [5, 10, 20, 30, 40]

Explanation

The list contains [10, 20, 30]. The addFirst(5) method places 5 before the first element, while addLast(40) places 40 after the last element.

2. Using add() Method to Add an Element at the Last

The add() method can also be used to insert an element at the end of a LinkedList. By default, when add(element) is called, the new element is appended to the end of the list.

Approach

  1. Create a LinkedList.
  2. Add elements using add().
  3. Use addFirst() to add an element at the beginning.
  4. Use add() to add an element at the end.
  5. Display the updated list.
Java
import java.util.LinkedList;

public class AddFirstLast{

    public static void main(String[] args)
    {

        LinkedList<String> list = new LinkedList<>();

        list.add("B");
        list.add("C");
        list.add("D");

        System.out.println("Original LinkedList: " + list);

        // Add element at the beginning
        list.addFirst("A");

        // Add element at the end
        list.add("E");

        System.out.println("Updated LinkedList: " + list);
    }
}

Output
Original LinkedList: [B, C, D]
Updated LinkedList: [A, B, C, D, E]

Explanation

The addFirst() method inserts "A" at index 0, while add("E") appends "E" to the end of the list.

Note: add() and addLast() both add an element at the end of a LinkedList.

3. Using add(index, element) Method

The add(index, element) method can be used when we want to insert an element at a particular position.

  • To insert an element at the beginning, use index 0.
  • To insert an element at the end, use the current size of the list as the index.

Syntax:

list.add(index, element);

Approach

  1. Create a LinkedList.
  2. Add some elements to the list.
  3. Use index 0 to insert an element at the beginning.
  4. Use list.size() to insert an element at the end.
  5. Print the updated list.
Java
import java.util.LinkedList;

public class AddFirstLast {

    public static void main(String[] args)
    {

        LinkedList<Integer> list = new LinkedList<>();

        list.add(20);
        list.add(30);
        list.add(40);

        System.out.println("Original LinkedList: " + list);

        // Insert at the first position
        list.add(0, 10);

        // Insert at the last position
        list.add(list.size(), 50);

        System.out.println("Updated LinkedList: " + list);
    }
}
Try It Yourself
redirect icon

Output
Original LinkedList: [20, 30, 40]
Updated LinkedList: [10, 20, 30, 40, 50]

Explanation: The program uses add(index, element) to insert elements at specific positions in the LinkedList.
list.add(0, 10) inserts 10 at the beginning, while list.add(list.size(), 50) inserts 50 at the end of the list.

Difference Between add(), addFirst() and addLast()

MethodPurposePosition
add()Adds an element to the listEnd of the list
addFirst()Adds an element at the beginningFirst position
addLast()Adds an element at the endLast position
add(index, element)Adds an element at a specific positionGiven index


Comment