![]() |
VOOZH | about |
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.
[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.
Syntax:
E remove(int index)
Example 2: Here, we use the remove() method to remove the element at the specified index from the list.
[A, B, C, D, E] Removed Element: B [A, C, D, E]
Syntax:
boolean remove(Objectitem)
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.
[Geeks, for, Geeks, 10, 20] [for, Geeks, 10]