Java program to remove elements from the linkedlist.

www.ig‮‬iftidea.com

Here's an example Java program that removes elements from a LinkedList:

import java.util.LinkedList;

public class RemoveElementsFromLinkedList {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<String>();
        list.add("apple");
        list.add("banana");
        list.add("orange");
        list.add("pear");
        list.add("kiwi");
        list.add("grape");

        System.out.println("Original LinkedList: " + list);

        list.remove("orange");
        System.out.println("LinkedList after removing 'orange': " + list);

        list.remove(1);
        System.out.println("LinkedList after removing element at index 1: " + list);

        list.clear();
        System.out.println("LinkedList after clearing all elements: " + list);
    }
}

This program first creates a LinkedList containing some elements. It then demonstrates three ways to remove elements from the list:

  1. The remove() method can be called with the element to remove as an argument. In this example, "orange" is removed from the list.
  2. The remove() method can be called with the index of the element to remove as an argument. In this example, the element at index 1 (which is "banana") is removed from the list.
  3. The clear() method is used to remove all elements from the list.

The program then prints the list after each removal operation to show the resulting list.