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 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.
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 classes import java.io.*; import java.util.LinkedList; Â
// Main class public 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]
Â
Time Complexity: O(n)
Auxiliary Space: O(n)
Type 2: remove(int index) MethodÂ
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 parameter import 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]
Â
Time Complexity: O(n)
Auxiliary Space: O(n)
Type 3: remove(Object O) Method
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 classes import java.io.*; import java.util.LinkedList; Â
// Main class public 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]
Â
Time complexity : O(n)Â
Auxiliary Space : O(n)