LinkedList remove() Method in Java

  1. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code to illustrate remove() method
    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>();
      
            // Using 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();
      
            // Print the final list
            System.out.println("Final LinkedList:" + list);
        }
    }

    chevron_right

    
    

    Output:

    LinkedList:[Geeks, for, Geeks, 10, 20]
    Final LinkedList:[for, Geeks, 10, 20]
    
  2. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code 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);
        }
    }

    chevron_right

    
    

    Output:

    LinkedList:[Geeks, for, Geeks, 10, 20]
    Final LinkedList:[Geeks, for, Geeks, 10]
    
  3. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code to illustrate remove() method
    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("Geeks");
            list.remove("20");
      
            // Print the final list
            System.out.println("Final LinkedList:" + list);
        }
    }

    chevron_right

    
    

    Output:

    LinkedList:[Geeks, for, Geeks, 10, 20]
    Final LinkedList:[for, Geeks, 10]
    


  4. My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    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.