Remove LinkedList elements

We can use remove() method of LinkedList to delete/remove elements from LinkedList

There are several methods to perform “remove” action in LinkedList


1) Object remove();
2) Object remove(int index);
3) boolean remove(Object o);
4) Object removeFirst();
5) boolean removeFirstOccurence(Object o);
6) Object removeLast();
7) boolean removeLastOccurence();

Object remove()

Removes the first element from the list

Example :

  1. import java.util.*;
  2. public class LinkedListRemove {
  3.     public static void main(String[] args) {
  4.     LinkedList<String> list = new LinkedList<String>();
  5.     list.add("Java");
  6. list.add("c++");
  7. list.add("python");
  8. list.add("c");
  9. System.out.println("List before removing elements");
  10. System.out.println(list);
  11. list.remove();
  12. System.out.println("List after removing elements");
  13. System.out.println(list);
  14.     }
  15. }
import java.util.*;
public class LinkedListRemove {
	public static void main(String[] args) {
	LinkedList<String> list = new LinkedList<String>();
	list.add("Java");
list.add("c++");
list.add("python");
list.add("c");
System.out.println("List before removing elements");
System.out.println(list);
list.remove();
System.out.println("List after removing elements");
System.out.println(list);
	}
}

Object remove(int index)

Removes the element at the specified position in the list.

Example :

  1. import java.util.*;
  2. public class LinkedListRemoveAtIndex {
  3.     public static void main(String[] args) {
  4.     LinkedList<String> list = new LinkedList<String>();
  5.     list.add("Java");
  6. list.add("c++");
  7. list.add("python");
  8. list.add("c");
  9. System.out.println("List before removing elements");
  10. System.out.println(list);
  11. list.remove(2);
  12. System.out.println("List after removing elements");
  13. System.out.println(list);
  14.     }
  15. }
import java.util.*;
public class LinkedListRemoveAtIndex {
	public static void main(String[] args) {
	LinkedList<String> list = new LinkedList<String>();
	list.add("Java");
list.add("c++");
list.add("python");
list.add("c");
System.out.println("List before removing elements");
System.out.println(list);
list.remove(2);
System.out.println("List after removing elements");
System.out.println(list);
	}
}
Note : We can see that element at index 2 in the list is removed

boolean remove(Object o)

Removes the first occurrence of the specified element from the list, if it is present.

Example :

  1. import java.util.*;
  2. public class LinkedListRemoveEle {
  3.     public static void main(String[] args) {
  4.     LinkedList<String> list = new LinkedList<String>();
  5.     list.add("Java");
  6. list.add("c++");
  7. list.add("python");
  8. list.add("c");
  9. System.out.println("List before removing elements");
  10. System.out.println(list);
  11. list.remove("c");
  12. System.out.println("List after removing elements");
  13. System.out.println(list);
  14.     }
  15. }
import java.util.*;
public class LinkedListRemoveEle {
	public static void main(String[] args) {
	LinkedList<String> list = new LinkedList<String>();
	list.add("Java");
list.add("c++");
list.add("python");
list.add("c");
System.out.println("List before removing elements");
System.out.println(list);
list.remove("c");
System.out.println("List after removing elements");
System.out.println(list);
	}
}
Note : We can see that, first occurrence of element “c” is removed from the list

Object removeFirst()

Removes the first element of the list.

Example :

  1. import java.util.*;
  2. public class LinkedListRemoveFirst {
  3.     public static void main(String[] args) {
  4.     LinkedList<String> list = new LinkedList<String>();
  5.     list.add("Java");
  6. list.add("c++");
  7. list.add("python");
  8. list.add("c");
  9. System.out.println("List before removing elements");
  10. System.out.println(list);
  11. list.removeFirst();
  12. System.out.println("List after removing elements");
  13. System.out.println(list);
  14.     }
  15. }
import java.util.*;
public class LinkedListRemoveFirst {
	public static void main(String[] args) {
	LinkedList<String> list = new LinkedList<String>();
	list.add("Java");
list.add("c++");
list.add("python");
list.add("c");
System.out.println("List before removing elements");
System.out.println(list);
list.removeFirst();
System.out.println("List after removing elements");
System.out.println(list);
	}
}


boolean removeFirstOccurence(Object o)

This will remove the first occurrence of given element

Example :

  1. import java.util.*;
  2. public class LinkedListRemoveFirstOccurenceEle {
  3.     public static void main(String[] args) {
  4.     LinkedList<String> list = new LinkedList<String>();
  5.     list.add("Java");
  6. list.add("c");
  7. list.add("c++");
  8. list.add("python");
  9. list.add("c");
  10. System.out.println("List before removing elements");
  11. System.out.println(list);
  12. list.removeFirstOccurrence("c");
  13. System.out.println("List after removing elements");
  14. System.out.println(list);
  15.     }
  16. }
import java.util.*;
public class LinkedListRemoveFirstOccurenceEle {
	public static void main(String[] args) {
	LinkedList<String> list = new LinkedList<String>();
	list.add("Java");
list.add("c");
list.add("c++");
list.add("python");
list.add("c");
System.out.println("List before removing elements");
System.out.println(list);
list.removeFirstOccurrence("c");
System.out.println("List after removing elements");
System.out.println(list);
	}
}
Note : We can see that first occurrence of “c” is removed from the list

Object removeLast()

This method removes the last element in the list

Example :

  1. import java.util.*;
  2. public class LinkedListRemoveLastEle {
  3.     public static void main(String[] args) {
  4.     LinkedList<String> list = new LinkedList<String>();
  5.     list.add("Java");
  6. list.add("c");
  7. list.add("c++");
  8. list.add("python");
  9. list.add("c");
  10. System.out.println("List before removing elements");
  11. System.out.println(list);
  12. list.removeLast();
  13. System.out.println("List after removing elements");
  14. System.out.println(list);
  15.     }
  16. }
import java.util.*;
public class LinkedListRemoveLastEle {
	public static void main(String[] args) {
	LinkedList<String> list = new LinkedList<String>();
	list.add("Java");
list.add("c");
list.add("c++");
list.add("python");
list.add("c");
System.out.println("List before removing elements");
System.out.println(list);
list.removeLast();
System.out.println("List after removing elements");
System.out.println(list);
	}
}


boolean removeLastOccurence(Object o)

This method removes the last occurrence of specified element

Example :

  1. import java.util.*;
  2. public class LinkedListRemoveLastOccurenceEle {
  3.     public static void main(String[] args) {
  4.     LinkedList<String> list = new LinkedList<String>();
  5.     list.add("Java");
  6. list.add("c");
  7. list.add("c++");
  8. list.add("python");
  9. list.add("c");
  10. System.out.println("List before removing elements");
  11. System.out.println(list);
  12. list.removeLastOccurrence("c");
  13. System.out.println("List after removing elements");
  14. System.out.println(list);
  15.     }
  16. }
import java.util.*;
public class LinkedListRemoveLastOccurenceEle {
	public static void main(String[] args) {
	LinkedList<String> list = new LinkedList<String>();
	list.add("Java");
list.add("c");
list.add("c++");
list.add("python");
list.add("c");
System.out.println("List before removing elements");
System.out.println(list);
list.removeLastOccurrence("c");
System.out.println("List after removing elements");
System.out.println(list);
	}
}

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on