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).
FAQs
- Where is the remove method present in java?
- What is the use of the remove method?
- What is an iterative remove method?
Remove method is present in the Java collection Framework.
Remove method removes the first occurrence of the element with the specified value.
The iterative remove method actually removes the current element in the collection.