In this article, we will discuss how to remove elements from LinkedList
There are different methods available in LinkedList class for removing elements as listed below,
- removeFirst(); – removes 1st or top element from LinkedList
- removeLast(); – removes last or bottom element from LinkedList
- remove(int index); – removes element from LinkedList at the specified index position, where index starts from ZERO
- remove(Object o); – removes specified element/object from LinkedList
- removeFirstOccurrence(Object o); – removes 1st occurrence of specified element from LinkedList
- removeLastOccurrence(Object o); – removes last occurrence of specified element from LinkedList
- remove(); – retrieves and removes the head/first element from LinkedList
Example for LinkedList remove operation
- There are initially 7 elements added to LinkedList
- after 1st removal using removeFirst(); there are only 6 elements remaining i.e.; removes “Sundar Pichai” from LinkedList
- In 2nd removal using removeLast(); method there are only 5 elements remaining i.e.; removes “Chandrasekaran Natarajan” from LinkedList
- Similarly, after 3rd removal using index-position of remove(int index); method, there are only 4 elements remaining in LinedkList i.e.; removes “Shantanu Narayen” from LinkedList
- At last after 4th removal using remove(Object o); only 3 elements remaining in the LinkedList which is lastly printed to console i.e.; removes “Shiv Nadar” from LinkedList
- Note: while going through below example, current LinkedList elements will always be previous iteration printed to console
package net.bench.resources.collection.example;
import java.util.LinkedList;
public class LinkedListRemoveOperation {
public static void main(String[] args) {
// creating LinkedList object of type String
LinkedList<String> ceo = new LinkedList<String>();
// adding elements to LinkedList object
ceo.add("Sundar Pichai");
ceo.add("Satya Nadella");
ceo.add("Shiv Nadar");
ceo.add("Shantanu Narayen");
ceo.add("Narayan Murthy");
ceo.add("Francisco D’Souza");
ceo.add("Chandrasekaran Natarajan");
// Iterating using enhanced for-loop
System.out.println("Iterating LinkedList elements - "
+ "Before Remove :\n");
for(String s : ceo){
System.out.println(s);
}
// 1.A removes FIRST element from current LinkedList
ceo.removeFirst();
// 1.B Iterating using enhanced for-loop
System.out.println("\n\nIterating CURRENT LinkedList elements - "
+ "After Removing 1st or top element :\n");
for(String s : ceo){
System.out.println(s);
}
// 2.A removes LAST element from current LinkedList
ceo.removeLast();
// 2.B Iterating using enhanced for-loop
System.out.println("\n\nIterating CURRENT LinkedList elements - "
+ "After Removing last or bottom element :\n");
for(String s : ceo){
System.out.println(s);
}
// 3.A removing element at 2nd index from current LinkedList
ceo.remove(2);
// 3.B Iterating using enhanced for-loop
System.out.println("\n\nIterating CURRENT LinkedList elements - "
+ "After Removing element at index-2 :\n");
for(String s : ceo){
System.out.println(s);
}
// 4.A removing element "Shiv Nadar" from current LinkedList
ceo.remove("Shiv Nadar");
// 4.B Iterating using enhanced for-loop
System.out.println("\n\nIterating CURRENT LinkedList elements - "
+ "After Removing element 'Shiv Nadar' :\n");
for(String s : ceo){
System.out.println(s);
}
}
}
Output:
Iterating LinkedList elements - Before Remove :
Sundar Pichai
Satya Nadella
Shiv Nadar
Shantanu Narayen
Narayan Murthy
Francisco D’Souza
Chandrasekaran Natarajan
Iterating CURRENT LinkedList elements - After Removing 1st or top element :
Satya Nadella
Shiv Nadar
Shantanu Narayen
Narayan Murthy
Francisco D’Souza
Chandrasekaran Natarajan
Iterating CURRENT LinkedList elements - After Removing last or bottom element :
Satya Nadella
Shiv Nadar
Shantanu Narayen
Narayan Murthy
Francisco D’Souza
Iterating CURRENT LinkedList elements - After Removing element at index-2 :
Satya Nadella
Shiv Nadar
Narayan Murthy
Francisco D’Souza
Iterating CURRENT LinkedList elements - After Removing element 'Shiv Nadar' :
Satya Nadella
Narayan Murthy
Francisco D’Souz
References:
Happy Coding !!
Happy Learning !!