Java LinkedList remove() Method with Examples

In the previous article, we have discussed about Java LinkedList containsAll() Method with Examples

In this article we are going to see the use of LinkedList remove() method in Java along with suitable examples.

Java LinkedList remove() Method with Examples

Let’s see remove() method with suitable examples.

remove()

This java.util.LinkedList.remove() method removes element from the beginning/head of linkedlist.

Syntax:

LinkedListName.remove()

Where,

  • LinkedListName refers to the name of your LinkedList.

Example: Java LinkedList remove() Method – Example with String Type LinkedList

Approach:

  • Create a new LinkedList of type String.
  • Add String elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Then remove specific element from the LinkedList using remove() method.
  • Print the new LinkedList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of string datatype
        LinkedList<String> l = new LinkedList<String>();
        // Adding some elements to the LinkedList
        l.add("Hello");
        l.add("this");
        l.add("is");
        l.add("an");
        l.add("example of remove() method");
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // removing the head/beginning element from the LinkedList
        l.remove();
        // Prints the new LinkedList elements
        System.out.println("The new elements of LinkedList are: "+l);
    }
}
Output:

The elements of LinkedList are: [Hello, this, is, an, example of remove() method]
The new elements of LinkedList are: [this, is, an, example of remove() method]

remove(Object o)

This java.util.LinkedList.remove(Object o) method removes specific element(first occurrence) from linkedlist.

Syntax:

LinkedListName.remove(object o)

Where,

  • LinkedListName refers to the name of your LinkedList.
  • remove(object o) refers to the specific element which is to be removed.

Example: Java LinkedList remove(Object o) Method – Example with String Type LinkedList

Approach:

  • Create a new LinkedList of type String.
  • Add String elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Then remove specific element from the LinkedList using remove(Object o) method.
  • Print the new LinkedList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of string datatype
        LinkedList<String> l = new LinkedList<String>();
        // Adding some elements to the LinkedList
        l.add("Hello");
        l.add("this");
        l.add("is");
        l.add("an");
        l.add("example of remove() method");
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // removing the specific element from the LinkedList
        l.remove("is");
        l.remove("example of remove() method");
        // Prints the new LinkedList elements
        System.out.println("The new elements of LinkedList are: "+l);
    }
}
Output:

The elements of LinkedList are: [Hello, this, is, an, example of remove() method]
The new elements of LinkedList are: [Hello, this, an]

remove(int index)

This java.util.LinkedList.remove(int index) method removes the element from specific index of the LinkedList.

Syntax:

LinkedListName.remove(int index)

Where,

  • LinkedListName refers to the name of your LinkedList.
  • int index refers to the specific index element which is to be removed.

Example: Java LinkedList remove(int index) Method – Example with Integer Type LinkedList

  • Create a new LinkedList of type Integer.
  • Add Integer elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Then remove the specific index elements from the LinkedList using remove(int index) method.
  • Print the new LinkedList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of Integer datatype
        LinkedList<Integer> l = new LinkedList<Integer>();
        // Adding some elements to the LinkedList
        l.add(2);
        l.add(4);
        l.add(6);
        l.add(8);
        l.add(10);
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // removing the specific index element from the LinkedList
        l.remove(2);
        // Prints the new LinkedList elements
        System.out.println("The new elements of LinkedList are: "+l);
    }
}
Output:

The elements of LinkedList are: [2, 4, 6, 8, 10]
The new elements of LinkedList are: [2, 4, 8, 10]

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Related Java Programs: