Remove Elements from the LinkedList in Java

Exercise:

Write a Java Program to remove elements from the LinkedList.

1.Remove using remove()

Click Here to View the Solution!
import java.util.LinkedList;

public class remove {
    public static void main(String[] args) {
        LinkedList<String> languages = new LinkedList<>();
        languages.add("Java");
        languages.add("Python");
        languages.add("JavaScript");
        languages.add("Kotlin");
        System.out.println("LinkedList: " + languages);

        // remove elements from index 3
        String str = languages.remove(3);
        System.out.println("Removed Element: " + str);
        System.out.println("Updated LinkedList: " + languages);
    }
}
Click Here to View the Output!
LinkedList: [Java, Python, JavaScript, Kotlin]
Removed Element: Kotlin
Updated LinkedList: [Java, Python, JavaScript]
Click Here to View the Explanation!
  • In this example, a string type LinkedList called languages is created firstly.
  • Elements are added to this LinkedList and then the list is printed on the screen.
  • Next, an element on index 3 is removed from the LinkedList using remove() method and stored in a String variable called str. remove() method takes an index value as a parameter.
  • The removed element, along with an updated LinkedList is then printed onto the screen.

2.Remove Using listIterator()

Click Here to View the Solution!
import java.util.ArrayList;
import java.util.ListIterator;

public class remove{
    public static void main(String[] args) {
        ArrayList<String> animals= new ArrayList<>();
        animals.add("Fish");
        animals.add("Cat");
        animals.add("Dog");
        System.out.println("LinkedList: " + animals);
        ListIterator<String> listIterate = animals.listIterator();
        listIterate.next();

        // चemove element returned by next()
        listIterate.remove();
        System.out.println("New LinkedList: " + animals);
    }
}
Click Here to View the Output!
 LinkedList: [Fish, Cat, Dog]
 New LinkedList: [Cat, Dog]
Click Here to View the Explanation!
  • In this example, a String type ArrayList called animals is created firstly.
  • Elements are added to this ArrayList and then the list is printed on the screen.
  • Then an instance of ListIterator class called listIterate is created.
  • next() method fetches the next element in the ArrayList.
  • remove() method removes that fetched element from the ArrayList.
  • The updated ArrayList is then printed onto the screen.

3. Remove all using clear()

Click Here to View the Solution!
import java.util.LinkedList;

public class remove{
    public static void main(String[] args) {
        LinkedList<String> languages = new LinkedList<>();
        languages.add("Java");
        languages.add("Python");
        languages.add("Swift");
        languages.add("PHP");
        System.out.println("LinkedList: " + languages);

        // remove all the elements
        languages.clear();
        System.out.println("LinkedList after clear(): " + languages);
    }
}
Click Here to View the Output!
LinkedList: [Java, Python, Swift, PHP]
LinkedList after clear(): []
Click Here to View the Explanation!
  • In this example, a string type LinkedList called languages is created firstly.
  • Elements are added to this LinkedList and then the list is printed on the screen.
  • clear() method is used to remove all elements from the LinkedList.
  • The updated LinkedList is then printed onto the screen.

%d bloggers like this: