Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

LinkedList remove() Method in Java

Last Updated on November 18, 2022 by Prepbytes

Basically in this article we will see how the remove method works in linked list. Remove method returns true if an object passed as a parameter is removed from the list. Lets just look in to the remove method in linked list.

linked list remove method in Java

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

Syntax of linked list remove method in Java:
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<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();

        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 linked list remove method in Java:
This method is used to remove the element at a specific index from the Linked List.

Syntax linked list remove method in Java:
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:
Remove method in linked list 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]

Conclusion

In the above article we have seen how remove method is used in linked list. Article explains the parameters, syntax and where it is applicable in linked list. 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).

Related Articles
Doubly circular linked list in data structure Application of doubly linked list
Applications of linked list Singly linked list vs doubly linked list
Advantages and disadvantages of linked list Doubly linked list all operations in C
Binary search in linked list Bubble sort linked list
Deletion in doubly linked list Delete the middle node of a linked list
Polynomial addition using linked list Find max value and min value in linked list
Insert a node at a specific position in a linked list Swap nodes in linked list
Add two numbers represented by linked lists Find starting point of loop in linked list
Merge sort linked list Delete a node from linked list at a given position
Remove duplicates from unsorted linked list Reverse a linked list in Python

FAQs

  1. Where is the remove method present in java?

  2. Remove method is present in the Java collection Framework.

  3. What is the use of the remove method?

  4. Remove method removes the first occurrence of the element with the specified value.

  5. What is an iterative remove method?

  6. The iterative remove method actually removes the current element in the collection.

Leave a Reply

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