LinkedList remove() Method in Java

Introduction

One of the most crucial data structures to learn while preparing for interviews is the linked list. In a coding interview, having a thorough understanding of Linked Lists might be a major benefit.

LinkedList remove() method

Description:
This method is used to remove and retrieve the head of this list.

Syntax:
LinkedList.remove()

Parameters:
This method does not take any parameters.

Return value:
Returns the removed element i.e, head of this list.

Throws:
NoSuchElementException - If the list is empty

import java.util.LinkedList;

public class Prepbytes {
    public static void main(String args[])
    {

        LinkedList list = new LinkedList<>();

        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);

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

        list.remove();

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

Output:
Original LinkedList:[1, 2, 3, 4, 5]
New LinkedList:[2, 3, 4, 5]

LinkedList remove(int index) method

Description:
This method is used to remove the element at a specific index from the Linked List.

Syntax:
LinkedList.remove(int index)

Parameter:
index - Index of the element to be removed from the LinkedList

Return value:
Return the element that is removed from the linked list.

Throws:
IndexOutOfBoundsException - If the specified index is less than 0 or greater than the size of this list.

import java.util.LinkedList;

public class Prepbytes {
    public static void main(String args[])
    {
        LinkedList<Integer> list = new LinkedList<>();

        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);

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

        list.remove(2);

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

Output
Original LinkedList:[1, 2, 3, 4, 5]
New LinkedList:[1, 2, 4, 5]

LinkedList remove(Object O) method

Description:
This method is used to remove the first occurrence of this object, which means the first occurrence of the element from the Linked List.

Syntax:
LinkedList.remove(Object O)

Parameter:
The parameter O specifies the element of the LinkedList which is to be removed.

Return value:
It will return true if the specified element is found in the LinkedList.

Throws:

  • ClassCastException – If the specified element has a type that is incompatible with this collection(optional).
  • NullPointerException – If the user specifies the null element and null is not allowed by this collection (optional).
  • UnsupportedOperationException – If the collection does not support the remove operation.
public class prepbytes {
    public static void main(String args[])
    {

        LinkedList<String> list = new LinkedList<>();

        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        list.add("e");

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

        list.remove("a");
        list.remove("c");;

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

Output
Original LinkedList:[a, b, c, d, e]
New LinkedList:[b, d, e]

This article explains how to use the LinkedList remove() method in Java.
.If you want to solve more questions on Linked List, which are curated by our expert mentors at PrepBytes, you can follow this link Prepbytes (Linked Lists).

Previous post Reverse a doubly-linked list in groups of given size
Next post ConcurrentLinkedQueue isEmpty() method

Leave a Reply

Your email address will not be published. Required fields are marked *