LinkedList remove() Method in Java
LinkedList as we all know is a way of storing data that contains sets of nodes where each node contains data and address part where address part is responsible for linking of nodes and hence forming a List over which now we can perform operations. Now here we want to remove a node/s using the remove() method of LinkedList class only.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Types of remove() method present inside this class:
- With no arguments inside
- Passing index as in arguments
- Passing object as in arguments
let us discuss each of them alongside implementing by providing a clean java program which is as follows:
Type 1: remove() Method
It is used to remove an element from a linked list. The element is removed from the beginning or head of the linked list.
Parameters: This function does not take any parameter.
Return Value: This method returns the head of the list or the element present at the head of the list.
LinkedList:[Geeks, for, Geeks, 10, 20] Final LinkedList:[for, Geeks, 10, 20]
Type 2: remove(int index) Method
It is used to remove an element from a linked list from a specific position or index.
Parameters: The parameter index is of integer data type and specifies the position of the element to be removed from the LinkedList.
Return Value: The element that has just been removed from the list.
LinkedList:[Geeks, for, Geeks, 10, 20] Final LinkedList:[Geeks, for, Geeks, 10]
Type 3: remove(Object O) Method
It is used to remove any particular element from the linked list.
Parameters: The parameter O is of the object type of linked list and specifies the element to be removed from the list.
Return Value: Returns true if the specified element is found in the list.
LinkedList:[Geeks, for, Geeks, 10, 20] Final LinkedList:[for, Geeks, 10]