Algorithm
-
Initialize the LinkedList: Create a LinkedList and add elements to it.
-
Prompt for Element to Remove: Ask the user for the element they want to remove from the LinkedList.
-
Traverse the LinkedList:
- Start at the head of the LinkedList.
- Traverse through the list until the target element is found or reach the end of the list.
-
Remove the Element:
- If the element is found, remove it by adjusting the pointers (previous node's next points to the next node).
-
Handle Edge Cases:
- Check if the element to be removed is at the beginning or end of the LinkedList and update the head or tail accordingly.
-
Display the Updated LinkedList: Print the elements of the modified LinkedList to show the removal.
Code Examples
#1 Code Example- Remove element using remove()
Code -
Java Programming
import java.util.LinkedList;
class Main {
public static void main(String[] args) {
LinkedList < String> languages = new LinkedList<>();
// add elements in LinkedList
languages.add("Java");
languages.add("Python");
languages.add("JavaScript");
languages.add("Kotlin");
System.out.println("LinkedList: " + languages);
// remove elements from index 1
String str = languages.remove(3);
System.out.println("Removed Element: " + str);
System.out.println("Updated LinkedList: " + languages);
}
}
Copy The Code &
Try With Live Editor
Output
Removed Element: Kotlin
Updated LinkedList: [Java, Python, JavaScript]
#2 Code Example- Using listIterator() Method
Code -
Java Programming
import java.util.ArrayList;
import java.util.ListIterator;
class Main {
public static void main(String[] args) {
ArrayList < String> animals= new ArrayList<>();
// add elements
animals.add("Dog");
animals.add("Cat");
animals.add("Horse");
System.out.println("LinkedList: " + animals);
// creating an object of ListIterator
ListIterator < String> listIterate = animals.listIterator();
listIterate.next();
// चemove element returned by next()
listIterate.remove();
System.out.println("New LinkedList: " + animals);
}
}
Copy The Code &
Try With Live Editor
Output
New LinkedList: [Cat, Horse]
#3 Code Example- Remove all elements using clear()
Code -
Java Programming
import java.util.LinkedList;
class Main {
public static void main(String[] args) {
LinkedList < String> languages = new LinkedList<>();
// add elements in LinkedList
languages.add("Java");
languages.add("Python");
languages.add("Swift");
System.out.println("LinkedList: " + languages);
// remove all the elements
languages.clear();
System.out.println("LinkedList after clear(): " + languages);
}
}
Copy The Code &
Try With Live Editor
Output
LinkedList after clear(): []
#4 Code Example- Using the removeIf() Method
Code -
Java Programming
import java.util.LinkedList;
class Main {
public static void main(String[] args) {
LinkedList < Integer> numbers = new LinkedList<>();
// add elements in LinkedList
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
System.out.println("LinkedList: " + numbers);
// remove all elements less than 4
numbers.removeIf((Integer i) -> i < 4);
System.out.println("Updated LinkedList: " + numbers);
}
}
Copy The Code &
Try With Live Editor
Output
Updated LinkedList: [4, 5]
Demonstration
Java Programing Example to Remove elements from the LinkedList-DevsEnv