Open In App

LinkedList remove() Method in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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:

  1. With no arguments inside
  2. Passing index as in arguments
  3. 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);
    }
}


Output: 

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


Output: 

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


Output: 

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

 

Time complexity : O(n) 
Auxiliary Space : O(n)



Last Updated : 06 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads