How to perform deletion in linked list

In a recent post, We have seen how to perform LinkedList insertion. In this post, we will see, How to delete a node from linked list? Unlike ArrayList, LinkedList doesn’t use continuous memory allocation to store the elements. Each element link with other so let’s see how can delete node at given position in a linked list. We have different methods to perform deletion in linked list.

Here is the table content of the article will we will cover this topic.
1. remove() method
2. remove(Object obj) method
3. remove(int index) method
4. removeFirst() method
5. removeLast() method
6. removeFirstOccurrence() method
7. removeLastOccurrence() method
8. poll() method
9. pollFirst() method

10. pollLast() method
11. pop() method
12.removeAll(Collection c)
13. removeIf(Predicate filter) method

1. remove() method

The remove() method is used to get and removes the first element of the LinkedList. When we call the remove() method, it retrieves and removes the first element(head) of the LinkedList. This method internally invokes the removeFirst() method. If LinkedList is empty then it throws NoSuchElementException. Let’s perform deletion in linked list.

public E remove();

Where, is the element which you want to remove from the LinkedList.
throws NoSuchElementException if this list is empty.

import java.util.Arrays;
import java.util.LinkedList;
public class ExampleOfLinkedList 
{
  public static void main(String[] args) 
  {
	// Creating a empty LinkedList  
    LinkedList<Integer> firstList = new LinkedList<Integer>(Arrays.asList(7,2,9));
    System.out.println("Elements: "+ firstList);
   
    Integer removedObject = firstList.remove();
    System.out.println("Removed Object: "+ removedObject);
    System.out.println("Elements: "+ firstList);
   }
}

Output: Elements: [7, 2, 9]
Removed Object: 7
Elements: [2, 9]

deletion in linked list

2. remove(Object obj) method

The remove(Object obj) method is used removes the first occurrence of the specified object from LinkedList. If the LinkedList doesn’t contain the specified element then it remains the same. But if specified object presents more than one time, It removes the object having the lowest index value that means the first occurrence of the object. Let’s see delete a node in linked list

    boolean remove(Object obj);

Where, Object represents the type of class in LinkedList.
obj is the element which you want to remove from the LinkedList.
return type:  Its return type is boolean. It can return either true or false.

import java.util.Arrays;
import java.util.LinkedList;
public class ExampleOfLinkedList 
{
  public static void main(String[] args) 
  {
	// Creating a empty LinkedList  
    LinkedList<Integer> firstList = new LinkedList<Integer>(Arrays.asList(7,2,9,2));
    System.out.println("Elements: "+ firstList);
   
    boolean isElementRemoved = firstList.remove(Integer.valueOf(2));
    System.out.println("Removed Object: "+ isElementRemoved);
    System.out.println("Elements: "+ firstList);
   }
}

Output: Elements: [7, 2, 9, 2]
Removed Object: true
Elements: [7, 9, 2]

deletion in linked list

3. remove(int index) method

The remove(int index) method is used to remove the element at the specified index from LinkedList. It retrieves and removes the object from LinkedList. If the index value is not correct, It will throw IndexOutOfBoundsException. Let’s see delete a node from linked list

public E remove(int index);

Where index, the index of the element to be removed.
E, the element that was removed from the LinkedList.
throw, IndexOutOfBoundsException if index is invalid.

import java.util.Arrays;
import java.util.LinkedList;
public class ExampleOfLinkedList 
{
  public static void main(String[] args) 
  {
	// Creating a empty LinkedList  
    LinkedList<Integer> firstList = new LinkedList<Integer>(Arrays.asList(7,2,9,2));
    System.out.println("Elements: "+ firstList);
   
    Integer removedElement = firstList.remove(2);
    System.out.println("Removed Object: "+ removedElement);
    System.out.println("Elements: "+ firstList);
   }
}

Output: Elements: [7, 2, 9, 2]
Removed Object: 9
Elements: [7, 2, 2]

deletion in linked list

4. removeFirst() method

This removeFirst() method is used to get and removes the first element from the LinkedList. It removes the head(first element) of LinkedList and also returns it. If LinkedList is empty it will throw NoSuchElementException. It works like remove() method and delete node in linked list.

public E removeFirst()

Where, is the class type of element which you want to remove from the LinkedList.

import java.util.Arrays;
import java.util.LinkedList;
public class ExampleOfLinkedList 
{
  public static void main(String[] args) 
  {
	// Creating a empty LinkedList  
    LinkedList<Integer> firstList = new LinkedList<Integer>(Arrays.asList(7,2,9));
    System.out.println("Elements: "+ firstList);
   
    Integer removedElement = firstList.removeFirst();
    System.out.println("Removed Object: "+ removedElement);
    System.out.println("Elements: "+ firstList);
   }
}

Output: Elements: [7, 2, 9]
Removed Object: 7
Elements: [2, 9]

5. removeLast() method

The removeLast() method is to get and remove the last element from the LinkedList. This method invokes the unlinkLast() method internally. If LinkedList is empty it will throw NoSuchElementException. Let’s perform deletion in linked list.

public E removeLast()

Where, is Class type of the element which you want to remove from the LinkedList.

import java.util.Arrays;
import java.util.LinkedList;
public class ExampleOfLinkedList 
{
  public static void main(String[] args) 
  {
	// Creating a empty LinkedList  
    LinkedList<Integer> firstList = new LinkedList<Integer>(Arrays.asList(7,2,9));
    System.out.println("Elements: "+ firstList);
   
    Integer removedElement = firstList.removeLast();
    System.out.println("Removed Object: "+ removedElement);
    System.out.println("Elements: "+ firstList);
   }
}

Output: Elements: [7, 2, 9]
Removed Object: 9
Elements: [7, 2]

deletion in linked list

6. removeFirstOccurrence() method

The removeFirstOccurrence() method is used to remove the first occurrence of the specified element from LinkedList. It internally invokes the remove(Object obj) method and its working is the same. It starts from the head and moves toward the tail. If the LinkedList does not contain any element, it is unchanged. Its return type is boolean.

public boolean removeFirstOccurrence();

return type:  Its return type is boolean. It can return either true or false.
It returns true after removal of first element from LinkedList else returns false.

import java.util.Arrays;
import java.util.LinkedList;
public class ExampleOfLinkedList 
{
  public static void main(String[] args) 
  {
	// Creating a empty LinkedList  
    LinkedList<Integer> firstList = new LinkedList<Integer>(Arrays.asList(7,2,9,2));
    System.out.println("Elements: "+ firstList);
   
    boolean isElementRemoved = firstList.removeFirstOccurrence(Integer.valueOf(2));
    System.out.println("Removed Object: "+ isElementRemoved);
    System.out.println("Elements: "+ firstList);
   }
}

Output: Elements: [7, 2, 9, 2]
Removed Object: true
Elements: [7, 9, 2]

7. removeLastOccurrence() method

The removeLastOccurrence() method is used to remove the last occurrence of the specified element from LinkedList. It starts from Tail and moves towards the head. If the LinkedList does not contain any element, it is unchanged. Its return type is boolean.

boolean removeLastOccurrence();

return type:  Its return type is boolean. It can return either true or false.
It returns true after removal of last element from LinkedList else returns false.

import java.util.Arrays;
import java.util.LinkedList;
public class ExampleOfLinkedList 
{
  public static void main(String[] args) 
  {
	// Creating a empty LinkedList  
    LinkedList<Integer> firstList = new LinkedList<Integer>(Arrays.asList(7,2,9,2));
    System.out.println("Elements: "+ firstList);
   
    boolean isElementRemoved = firstList.removeLastOccurrence(Integer.valueOf(2));
    System.out.println("Removed Object: "+ isElementRemoved);
    System.out.println("Elements: "+ firstList);
   }
}

Output: Elements: [7, 2, 9, 2]
Removed Object: true
Elements: [7, 2, 9]

deletion in linked list

8. poll() method

The poll() method is used to get and remove the first element from the ArrayList. This method retrieves and returns the first element from LinkedList. It is declared in the Queue interface. It works similarly as remove() method.

 public E poll()

Where, E represents the type of elements in LinkedList.
return null, if this LinkedList is empty.

import java.util.Arrays;
import java.util.LinkedList;
public class ExampleOfLinkedList 
{
  public static void main(String[] args) 
  {
	// Creating a empty LinkedList  
    LinkedList<Integer> firstList = new LinkedList<Integer>(Arrays.asList(7,2,9));
    System.out.println("Elements: "+ firstList);
   
    Integer removedObject = firstList.poll();
    System.out.println("Removed Object: "+ removedObject);
    System.out.println("Elements: "+ firstList);
   }
}

Output: Elements: [7, 2, 9]
Removed Object: 7
Elements: [2, 9]

9. pollFirst() method

The pollFirst() method removes and returns the first element from LinkedList. If LinkedList is empty it returns null.

E pollFirst();

Where, E represents the type of elements in LinkedList.
return null, if this LinkedList is empty.

import java.util.Arrays;
import java.util.LinkedList;
public class ExampleOfLinkedList 
{
  public static void main(String[] args) 
  {
	// Creating a empty LinkedList  
    LinkedList<Integer> firstList = new LinkedList<Integer>(Arrays.asList(7,2,9));
    System.out.println("Elements: "+ firstList);
   
    Integer removedObject = firstList.pollFirst();
    System.out.println("Removed Object: "+ removedObject);
    System.out.println("Elements: "+ firstList);
   }
}

Output: Elements: [7, 2, 9]
Removed Object: 7
Elements: [2, 9]

10 pollLast() method

The pollLast() method removes and returns the last element from LinkedList.It work like pollFirst() method but except it removes the tail of LinkedList. If LinkedList is empty it returns null.

public E pollLast()
import java.util.Arrays;
import java.util.LinkedList;
public class ExampleOfLinkedList 
{
  public static void main(String[] args) 
  {
	// Creating a empty LinkedList  
    LinkedList<Integer> firstList = new LinkedList<Integer>(Arrays.asList(7,2,9));
    System.out.println("Elements: "+ firstList);
   
    Integer removedObject = firstList.pollLast();
    System.out.println("Removed Object: "+ removedObject);
    System.out.println("Elements: "+ firstList);
   }
}

Output: Elements: [7, 2, 9]
Removed Object: 9
Elements: [7, 2]

11. pop() method

The pop() method is used to removes and return the first element from LinkedList. Its return type is E. It retrieves the first element and also removes it from the LinkedList.

 E pop();

Where, E represents the type of elements in LinkedList.
throws NoSuchElementException if this list is empty.

import java.util.Arrays;
import java.util.LinkedList;
public class ExampleOfLinkedList 
{
  public static void main(String[] args) 
  {
	// Creating a empty LinkedList  
    LinkedList<Integer> firstList = new LinkedList<Integer>(Arrays.asList(7,2,9));
    System.out.println("Elements: "+ firstList);
   
    Integer removedObject = firstList.pop();
    System.out.println("Removed Object: "+ removedObject);
    System.out.println("Elements: "+ firstList);
   }
}

Output: Elements: [7, 2, 9]
Removed Object: 7
Elements: [2, 9]

12. removeAll(Collection c)

The removeAll(Collection c) method is used to remove all elements of the given collection from LinkedList. It removes all elements from LinkedList whether it is duplicated in LinkedList or not. This method iterates over the given collection and matches each object in the LinkedList. If the object is presented in LinkedList, it will remove that object from the collection. It returns boolean value. This method is declared in the List interface.

public boolean removeAll(Collection c)

return type, It returns a boolean value.
c, The collection which you wan to remove.
UnsupportedOperationException, If the iterator is not able to remove any element.
ClassCastException, If given collection is incompatible.
NullPointerException, If given collection is NULL.

import java.util.Arrays;
import java.util.LinkedList;
public class ExampleOfLinkedList 
{
  public static void main(String[] args) 
  {
	// Creating a empty LinkedList  
    LinkedList<Integer> firstList = new LinkedList<Integer>(Arrays.asList(7,2,9,2));
    LinkedList<Integer> secondList = new LinkedList<Integer>(Arrays.asList(2,9));
       
    System.out.println("Elements Before: "+ firstList);
    firstList.removeAll(secondList);
    System.out.println("Elements After: "+ firstList);
   }
}

Output: Elements Before: [7, 2, 9, 2]
Elements After: [7]

deletion in linked list

13. removeIf(Predicate filter) method

The removeIf(Predicate filter) method is also used to remove the element from the LinkedList. It takes an argument of Predicate type. The predicate was introduced in Java 8. This method removes only those elements that match the condition of Predicate. It is the default method defined in the Collection interface. Let see the deletion in linked list.

default boolean removeIf(Predicate filter)

access modifier, It is the default method of the Collection interface.
return type, It returns a boolean value.
filter, a predicate
NullPointerException, if the specified filter is null
UnsupportedOperationException if elements cannot be removed

import java.util.Arrays;
import java.util.LinkedList;
public class ExampleOfLinkedList 
{
  public static void main(String[] args) 
  {
	// Creating a empty LinkedList  
    LinkedList<Integer> firstList = new LinkedList<Integer>(Arrays.asList(7,2,9,2));
    LinkedList<Integer> secondList = new LinkedList<Integer>(Arrays.asList(2,9));
       
    System.out.println("Elements Before: "+ firstList);
    firstList.removeIf(n -> n >2);
    System.out.println("Elements After: "+ firstList);
   }
}

Output: Elements Before: [7, 2, 9, 2]
Elements After: [2, 2]

Leave a Comment