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