LinkedList remove() Method in Java
-
The Java.util.LinkedList.remove() method is used to remove an element from a linked list. The element is removed from the beginning or head of the linked list.
Syntax:
LinkedList.remove()
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.
Below program illustrate the Java.util.LinkedList.remove() method:
// Java code to illustrate remove() methodimportjava.io.*;importjava.util.LinkedList;publicclassLinkedListDemo {publicstaticvoidmain(String args[]){// Creating an empty LinkedListLinkedList<String> list =newLinkedList<String>();// Using add() method to add elements in the listlist.add("Geeks");list.add("for");list.add("Geeks");list.add("10");list.add("20");// Output the listSystem.out.println("LinkedList:"+ list);// Remove the head using remove()list.remove();// Print the final listSystem.out.println("Final LinkedList:"+ list);}}chevron_rightfilter_noneOutput:LinkedList:[Geeks, for, Geeks, 10, 20] Final LinkedList:[for, Geeks, 10, 20]
-
The Java.util.LinkedList.remove(int index) method is used to remove an element from a linked list from a specific position or index.
Syntax:
LinkedList.remove(int 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: This method returns the element that has just been removed from the list.
Below program illustrate the Java.util.LinkedList.remove(int index) method:
// Java code to illustrate remove() when position of// element is passed as parameterimportjava.io.*;importjava.util.LinkedList;publicclassLinkedListDemo {publicstaticvoidmain(String args[]){// Creating an empty LinkedListLinkedList<String> list =newLinkedList<String>();// Use add() method to add elements in the listlist.add("Geeks");list.add("for");list.add("Geeks");list.add("10");list.add("20");// Output the listSystem.out.println("LinkedList:"+ list);// Remove the head using remove()list.remove(4);// Print the final listSystem.out.println("Final LinkedList:"+ list);}}chevron_rightfilter_noneOutput:LinkedList:[Geeks, for, Geeks, 10, 20] Final LinkedList:[Geeks, for, Geeks, 10]
-
The Java.util.LinkedList.remove(Object O) method is used to remove any particular element from the linked list.
Syntax:
LinkedList.remove(Object O)
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.
Below program illustrate the Java.util.LinkedList.remove(Object O) method:
// Java code to illustrate remove() methodimportjava.io.*;importjava.util.LinkedList;publicclassLinkedListDemo {publicstaticvoidmain(String args[]){// Creating an empty LinkedListLinkedList<String> list =newLinkedList<String>();// Use add() method to add elements in the listlist.add("Geeks");list.add("for");list.add("Geeks");list.add("10");list.add("20");// Output the listSystem.out.println("LinkedList:"+ list);// Remove the head using remove()list.remove("Geeks");list.remove("20");// Print the final listSystem.out.println("Final LinkedList:"+ list);}}chevron_rightfilter_noneOutput:LinkedList:[Geeks, for, Geeks, 10, 20] Final LinkedList:[for, Geeks, 10]
- ArrayList and LinkedList remove() methods in Java with Examples
- LinkedList pop() Method in Java
- LinkedList add() Method in Java
- LinkedList set() Method in Java
- LinkedList get() Method in Java
- LinkedList contains() Method in Java
- LinkedList spliterator() method in Java
- LinkedList addFirst() Method in Java
- LinkedList removeLast() Method in Java
- LinkedList removeFirst() Method in Java
- LinkedList addAll() Method in Java
- LinkedList removeFirstOccurrence() Method in Java
- LinkedList indexOf() method in Java
- LinkedList addLast() Method in Java
- LinkedList clone() Method in Java
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



