Java Program to Remove elements from the LinkedList.
What is LinkedList ?
In this Article, We will going to write a Program to Remove elements from the LinkedList using various methods.
A linked list is a linear data structure in which each element (called a node) stores a reference to the next element in the list. Linked lists are dynamic data structures, which means that their size can change during the execution of a program. In contrast, arrays have a fixed size, which means that you need to specify the size of the array when you create it and you cannot change it later.
Java Program to Remove Elements From LinkedList:
We can remove an element from the LinkedList in java using various ways and functions, Some of the common function to remove an element are:
- remove()
- listIterator()
- clear()
- removeif()
Program to Remove an Element using remove():
// Importing all the required packages
import java.util.*;
public class Main{
public static void main(String[] args){
// Create a linked list
LinkedList list = new LinkedList<>();
// Adding elements to the linkedlist
list.add(10);
list.add(20);
list.add(30);
list.add(40);
// Before removing the element
System.out.println("List Before Removing the Element: " + list);
// Remove an element from the linked list
int removedelement = list.remove(3);
System.out.println(removedelement + " has been removed from the linked list.");
// After removing the element
System.out.println("List after Removing the Element: " + list);
}
}
Output: List Before Removing the Element: [10, 20, 30, 40] 40 has been removed from the linked list. List after Removing the Element: [10, 20, 30]
Program to Remove an Element using listIterator():
import java.util.LinkedList;
import java.util.ListIterator;
public class Main{
public static void main(String[] args) {
// Create a linked list
LinkedList list = new LinkedList<>();
// Adding Elements to the linkedlist
list.add(10);
list.add(20);
list.add(30);
list.add(40);
System.out.println("LinkedList Before removing the Element: " + list);
// Remove an element from the linked list using ListIterator
int elementToRemove = 30;
ListIterator iter = list.listIterator();
while (iter.hasNext()) {
if (iter.next() == elementToRemove) {
iter.remove();
System.out.println(elementToRemove + " has been removed from the linked list.");
break;
}
}
if (!iter.hasNext()) {
System.out.println(elementToRemove + " is not present in the linked list.");
}
// Print the updated linked list
System.out.println("Updated linked list: " + list);
}
}
Output: LinkedList Before removing the Element: [10, 20, 30, 40] 30 has been removed from the linked list. Updated linked list: [10, 20, 40]
Program to Remove Elements using clear():
// Importing all the required Packages
import java.util.*;
public class Main{
public static void main(String[] args) {
// Create a linked list
LinkedList list = new LinkedList<>();
// Adding elements to the list
list.add(10);
list.add(20);
list.add(30);
list.add(40);
list.add(50);
// Remove all elements from the linked list using clear()
list.clear();
System.out.println("All elements have been removed from the linked list.");
// Print the updated linked list
System.out.println("Updated linked list: " + list);
}
}
Output: All elements have been removed from the linked list. Updated linked list: []
Program to Remove Elements using removeif():
// Importing all the required packages
import java.util.*;
public class Main{
public static void main(String[] args) {
// Create a linked list
LinkedList list = new LinkedList<>();
// Adding elements to the linkedlist
list.add(10);
list.add(15);
list.add(20);
list.add(30);
list.add(35);
System.out.println("Before Removing Elements: " + list);
// Removing elements from the linked list using removeIf()
list.removeIf(element -> element % 10 == 0);
System.out.println("Elements divisible by 10 have been removed from the linked list.");
// Print the updated linked list
System.out.println("Updated linked list: " + list);
}
}
Output: Before Removing Elements: [10, 15, 20, 30, 35] Elements divisible by 10 have been removed from the linked list. Updated linked list: [15, 35]
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Login/Signup to comment