Skip to content
Related Articles

Related Articles

LinkedList remove() Method in Java
  • Difficulty Level : Easy
  • Last Updated : 10 Dec, 2018
GeeksforGeeks - Summer Carnival Banner
  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:






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




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




    // 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);
        }
    }
    Output:
    LinkedList:[Geeks, for, Geeks, 10, 20]
    Final LinkedList:[for, Geeks, 10]
    

    Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

    My Personal Notes arrow_drop_up
Recommended Articles
Page :