LinkedList remove() Method in Java - GeeksforGeeks

23 Nov, 2021
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.
Illustration:
Types of remove() method present inside this class:
With no arguments insidePassing index as in argumentsPassing object as in arguments
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: 
It 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.
Example:
Java
// Java Program to Illustrate remove() method// of LinkedList class// Default removal from the last of List  // Importing required classesimport java.io.*;import java.util.LinkedList;  // Main classpublic class GFG {      // Main driver method    public static void main(String args[])    {          // Creating an empty LinkedList of String type        LinkedList<String> list = new LinkedList<String>();          // Adding elements in the list        // Using add() method        list.add("Geeks");        list.add("for");        list.add("Geeks");        list.add("10");        list.add("20");          // Printing the elements inside LinkedList        System.out.println("LinkedList:" + list);          // Removing the head from List        // using remove() method        list.remove();          // Printing the final elements inside Linkedlist        System.out.println("Final LinkedList:" + list);    }}
LinkedList:[Geeks, for, Geeks, 10, 20]
Final LinkedList:[for, Geeks, 10, 20]
 
It 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: The element that has just been removed from the list.
Example
Java
// Java Program to Illustrate remove() when position of// element is passed as parameterimport java.io.*;import java.util.LinkedList;  public class LinkedListDemo {    public static void main(String args[])    {          // Creating an empty LinkedList        LinkedList<String> list = new LinkedList<String>();          // Use add() method to add elements in the list        list.add("Geeks");        list.add("for");        list.add("Geeks");        list.add("10");        list.add("20");          // Output the list        System.out.println("LinkedList:" + list);          // Remove the head using remove()        list.remove(4);          // Print the final list        System.out.println("Final LinkedList:" + list);    }}
LinkedList:[Geeks, for, Geeks, 10, 20]
Final LinkedList:[Geeks, for, Geeks, 10]
 
It 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. 
Example
Java
// Java Program to Illustrate remove() method  // Importing required classesimport java.io.*;import java.util.LinkedList;  // Main classpublic class GFG {      // Main driver method    public static void main(String args[])    {        // Creating an empty LinkedList of string type        LinkedList<String> list = new LinkedList<String>();          // Adding elements in the list        // using add() method        list.add("Geeks");        list.add("for");        list.add("Geeks");        list.add("10");        list.add("20");          // Printing the elements before removal        // inside above created LinkedList object        System.out.println("LinkedList:" + list);          // Removing the head        // using remove() method        list.remove("Geeks");        list.remove("20");          // Printing the final elements after removal        // inside above LinkedList object        System.out.println("Final LinkedList:" + list);    }}
LinkedList:[Geeks, for, Geeks, 10, 20]
Final LinkedList:[for, Geeks, 10]
 
solankimayank
Java - util package
Java-Collections
Java-Functions
java-LinkedList
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Stream In Java
Interfaces in Java
How to iterate any Map in Java
Initialize an ArrayList in Java
ArrayList in Java
Stack Class in Java
Singleton Class in Java
Multidimensional Arrays in Java