Remove element at a specified index in LinkedList
In the previous post, we have seen how to add element at the beginning and end of the LinkedList. In this post, we will see how to remove element at a specified index in LinkedList.
We have method remove(int index) , which is used to remove the element from the LinkedList.
Syntax:
public E remove(int index)
Example:
import java.util.LinkedList; public class LinkedListEx { public static void main(String[] args) { LinkedList<String> fruits1 = new LinkedList<String>(); fruits1.add("Orange"); fruits1.add("Mango"); fruits1.add("Apple"); fruits1.add("Grapes"); fruits1.remove(0); // Orange will be removed System.out.println("Removed element at index 0 :"+ fruits1); } }
Output:
Removed element at index 0 :[Mango, Apple, Grapes]
Questions/Suggestions
Have any question or suggestion for us?Please feel free to post in Q&A Forum