LinkedList remove() Method in Java
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.
// 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:

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)
- Parameter: Index is the position of the element we want to remove from the list.
- Return Type: This method return the element that was removed from the list.
Example 2: Here, we use the remove()
method to remove the element at the specified index from the list.
// 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.
- It will return true, if the specified element is removed form the list
- It will return false, if the specified element is not found in the list.
Example 3: Here, we use the remove() method to remove any specified element from the LinkedList.
// 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]