Table of contents
1.
Introduction
2.
remove()
2.1.
Example
2.2.
Program
3.
remove(int INDEX)
3.1.
Example
3.2.
Program
4.
remove(Object OBJ)
4.1.
Example
4.2.
Program
5.
Time Complexity of remove() method
5.1.
Example
6.
Frequently Asked Questions
6.1.
What is the Time Complexity for Searching an element in a Linked List?
6.2.
Is it easier to add a new element in a Linked List as compared to an array?
7.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Linked List remove() Method in Java

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Linked List

Introduction

What a fantastic job James Gosling did by creating Java. According to Github, Java is one of the most popular programming languages, with about nine million developers using it.

It's a straightforward, object-oriented programming language that's both secure and robust. The Java runtime is fantastic, and it's one of the best languages for data structures and algorithms. In the Java library, practically every data structure is predefined, and a linked list is one of them.

Today we'll look at the Linked List’s remove() method in Java.

There are three versions of the Linked List remove method in Java.

  1. remove()- This takes no parameters and removes the first node of the linked list.
  2. remove(int INDEX)- This takes one parameter INDEX and removes the INDEXth node in the linked list.
  3. remove(Object OBJ)- This one takes one parameter, OBJ, which is the object to be removed from the linked list.

Let’s explore each variation of remove() one by one.

See, Application of Linked Lists

remove()

The Java.util.LinkedList.remove() method will remove the first element from the linked list.  

Example

Illustration Image

Program

import java.io.*;

// Library that includes all Linked List's methods.
import java.util.LinkedList;

// Class that removes the first element of the linked list using the ’remove()’ method.
public class LinkedListRemove{
   public static void main(String args[]){
   
       // Initialize the Linked List.
       LinkedList<String> list = new LinkedList<String>();
       
       // Inserting nodes in the linked list it usig ’add()’ method.
       list.add("CodingNinja");
       list.add("Coding Ninjas Studio");
       list.add("LinkedList");
       
       // Printing the List before Remove.
       System.out.println("List before removal: " + list);
       
       // Removing the head node using remove() method.
       list.remove();
       
       // Printing the List after removal.
       System.out.println("List after removal: " + list);
   }
}
You can also try this code with Online Java Compiler
Run Code

Output

List before removal: [CodingNinja, Coding Ninjas Studio, LinkedList]
List after removal: [Coding Ninjas Studio, LinkedList]

Recommended Topic, Floyds Algorithm

remove(int INDEX)

The Java.util.LinkedList.remove(int INDEX) method remove the INDEXth node from the linked list. 

Example

Illustration Image

Program

import java.io.*;
// Library that includes all Linked List's methods.
import java.util.LinkedList;

// Class that removes the INDEXth element of the linked list using the remove(int INDEX) method.
public class LinkedListRemove{
   public static void main(String args[]){
   
       // Initialize the Linked List.
       LinkedList<String> list = new LinkedList<String>();
       
       // Inserting nodes in the linked list it using ’add()’ method.
       list.add("CodingNinja");
       list.add("Coding Ninjas Studio");
       list.add("LinkedList");
       list.add("Tree");
       list.add("Graph");
       
       // Printing the list before removal.
       System.out.println("List before removal: " + list);
       
       // Removing the 2nd index element using ’remove(int INDEX)’ method.
       list.remove(2);
       
       // Printing the list after removal.
       System.out.println("List after removal: " + list);
   }
}
You can also try this code with Online Java Compiler
Run Code

Output

List before removal: [CodingNinja, Coding Ninjas Studio, LinkedList, Tree, Graph]
List after removal: [CodingNinja, Coding Ninjas Studio, Tree, Graph]

remove(Object OBJ)

The Java.util.LinkedList.remove(object OBJ) method remove an object, OBJ from the linked list.

Example

Illustration Image

Program

import java.io.*;
// Library that includes all Linked List's methods.
import java.util.LinkedList;

// Class that removes the OBJ object from the linked list using the ’remove(Object OBJ)’ method.
public class LinkedListRemove{
   public static void main(String args[]){
   
       // Initialize the Linked List.
       LinkedList<String> list = new LinkedList<String>();
       
       // Inserting nodes in the linked list using ‘add()’ method.
       list.add("CodingNinja");
       list.add("Coding Ninjas Studio");
       list.add("Tree");
       list.add("Graph");
       
       // Printing the list before removal.
       System.out.println("List before removal: " + list);
       
       // Removing the “Graph” using ’remove(Object OBJ)’ method.
       list.remove("Graph");
       
       // Printing the List after removal.
       System.out.println("List after removal: " + list);
   }
}
You can also try this code with Online Java Compiler
Run Code

Output

List before Removal: [CodingNinja, Coding Ninjas Studio, Tree, Graph]
List after Removal: [CodingNinja, Coding Ninjas Studio, Tree]

Time Complexity of remove() method

  1. Worst Case - In the worst case, remove()remove(int INDEX)remove(object OBJ) takes O(N) time. This is because we have to shift ‘N - 1’ elements after removing any element as the list is internally implemented as arrays.
  2. Best Case - If we have to remove the last element i.e., ‘N’ - 1 then the time complexity is O(1). This is not true in the case of remove(object OBJ) since we have to search the object in the array.

Example

Illustration Image

Frequently Asked Questions

What is the Time Complexity for Searching an element in a Linked List?

O(N), where N is the size of the List

Is it easier to add a new element in a Linked List as compared to an array?

Yes. As the size of an array is fixed upon the time of declaration, if we have to add more elements to it we will have to declare another array with a larger size and shift all the existing elements there and then add a new element. Whereas in Linked Lists, we can just create a new node.

Conclusion 

We've discussed all variations to the linked list remove method in Java. I hope you had fun reading the article. We offer a range of such blogs on the Coding Ninjas Platform. In addition, Coding Ninjas recently released the Coding Ninjas Studio Test Series, a test series designed specifically for acing tech interviews.

Recommended Reading:

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

To study more about Linked Lists, refer to Applications Of Linked Lists.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Thank you for taking the time to read this. I hope this blog has been beneficial to you.

Live masterclass