Delete a specific item from LinkedList in Java

We shared an article that explains how to remove an element from a specific index. In this tutorial, we'll see how search for element  with its value and delete it  de LinkedList.

LinkedList has a method that takes the value of the object to be deleted as a parameter:

public boolean remove(Object o): Removes the first occurrence of the object o from the list. If it is present then the list contains the item you are looking for and returns true, otherwise it returns false.

Example:

import java.util.LinkedList; 

public class RemoveElement {

public static void main(String[] args) {

LinkedList llist = new LinkedList();

llist.add("o1");
llist.add("o2");
llist.add("o3");
llist.add("o4");
llist.add("o5");

System.out.println("Before deletion: "+llist);

boolean b = llist.remove("o2");
// displays true if it exists and has been successfully deleted
System.out.println("deleted item: "+b);

System.out.println("After deletion(first): "+llist);
}
}
Result:

Before deletion: [o1, o2, o3, o4, o5]
deleted item: true
After deletion(first): [o1, o3, o4, o5]

References:
LinkedList with example