LinkedList remove() Method in Java (original) (raw)

Last Updated : 11 Jul, 2025

In Java, the **remove() method of the LinkedList class removes an element from the list, either by specifying its index or by providing its value.

**Example 1: Here, we use the remove() method to remove element from the LinkedList of Strings. By default the remove() will remove the beginning element(head) of the list.

Java `

// Use of remove() in LinkedList of Strings import java.util.LinkedList;

class Geeks { public static void main(String[] args) {

    // Create a LinkedList of strings
    LinkedList<String> l = new LinkedList<String>();

    // Use add() method to 
    // add elements in the list
    l.add("Geeks");
    l.add("for");
    l.add("Geeks");

    System.out.println("" + l);

    // remove() method will remove 
    // the head of the LinkedList
    l.remove();

    // Printing the final LinkedList
    System.out.println("" + l);
}

}

`

Output

[Geeks, for, Geeks] [for, Geeks]

**Illustration:

remove() LinkedList

**Now there are two versions of the remove() method i.e. remove an element by index and remove an element by value.

**1. Remove an Element by Index

**Syntax:

E remove(int index)

Example 2: Here, we use theremove()** method **to remove the element at the specified index from the list.

Java `

// Remove the element from the // specified index using remove() import java.util.LinkedList;

class Geeks { public static void main(String[] args) {

    // Creating an empty LinkedList
    LinkedList<String> l = new LinkedList<>();
  
    // Use add() to add 
    // elements in the list
    l.add("A");
    l.add("B");
    l.add("C");
    l.add("D");
    l.add("E");

    System.out.println("" + l);

    // Removing the element from 
    // the list by specifiying the index
    System.out.println("Removed Element: "
                       + l.remove(1));

    // Displaying the new LinkedList
    System.out.println("" + l);
}

}

`

Output

[A, B, C, D, E] Removed Element: B [A, C, D, E]

2. Remove an Element by Value

**Syntax:

boolean remove(Object item)

**Parameter: item - It is the element we want to delete from the LinkedList.

**Return Type: This method will return a boolean value.

**Example 3: Here, we use the **remove() method **to remove any specified element from the LinkedList.

Java `

// Remove any specified element // from the LinkedList using remove() import java.util.LinkedList;

public class Geeks { public static void main(String args[]) {

    // Creating an empty LinkedList
    LinkedList<String> l = new LinkedList<String>();

    // use add() method to add 
    // elements in the list
    l.add("Geeks");
    l.add("for");
    l.add("Geeks");
    l.add("10");
    l.add("20");

    System.out.println("" + l);

    // use remove() method to 
    // remove element from the list
    l.remove("Geeks");
    l.remove("20");

    // Displaying the Final LinkedList
    System.out.println("" + l);
}

}

`

Output

[Geeks, for, Geeks, 10, 20] [for, Geeks, 10]