Java LinkedList removeFirst() Method with Examples

In the previous article, we have discussed about Java LinkedList removeLast() Method with Examples

In this article we are going to see the use of Java LinkedList removeFirst() method along with suitable examples.

Java LinkedList removeFirst() Method with Examples

This java.util.LinkedList.removeFirst() method is used to remove any single element from the first/head position of the LinkedList. It returns the first element which got removed.

Syntax:

LinkedListName.removeFirst()

Where,

  • LinkedListName refers to the name of your LinkedList.

Let’s see removeFirst() method with suitable examples.

Example-1: Java LinkedList removeFirst() Method – Example with String Type LinkedList

Approach:

  • Create a new LinkedList of type String.
  • Add String elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Then remove the first element from the LinkedList using removeFirst() method.
  • Print the new LinkedList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of string datatype
        LinkedList<String> l = new LinkedList<String>();
        // Adding some elements to the LinkedList
        l.add("Hello");
        l.add("this");
        l.add("is");
        l.add("an");
        l.add("example of removeFirst() method");
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // removing the first element from the LinkedList
        l.removeFirst();
        // Prints the new LinkedList elements
        System.out.println("The new elements of LinkedList are: "+l);
    }
}
Output:

The elements of LinkedList are: [Hello, this, is, an, example of removeFirst() method]
The new elements of LinkedList are: [this, is, an, example of removeFirst() method]

Example-2: Java LinkedList removeFirst() Method – Example with Integer Type LinkedList

Approach:

  • Create a new LinkedList of type Integer.
  • Add Integer elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Then remove the first element from the LinkedList using removeFirst() method.
  • Print the new LinkedList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of Integer datatype
        LinkedList<Integer> l = new LinkedList<Integer>();
        // Adding some elements to the LinkedList
        l.add(2);
        l.add(4);
        l.add(6);
        l.add(8);
        l.add(10);
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // removing the first element from the LinkedList
        l.removeFirst();
        // Prints the new LinkedList elements
        System.out.println("The new elements of LinkedList are: "+l);
    }
}
Output:

The elements of LinkedList are: [2, 4, 6, 8, 10]
The new elements of LinkedList are: [4, 6, 8, 10]

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Related Java Programs: