- Linked list in java is a doubly-linked list.
- Insertion order is maintained in a linked list.
- LinkedList extends AbstractSequentialList class & implements List, Deque, Cloneable, Serializable interfaces.
- LinkedList class is not thread safe.
- In multi threaded environment, it should be synchronized externally.
- The iterators returned by LinkedList class’s iterator and listIterator methods are fail-fast.
1. Methods to remove or delete element(s)/node(s) in LinkedList collection.
No. | Method Name | Description |
---|---|---|
1 | E remove() | Retrieves and removes the head (first element) of this list. |
2 | E remove(int index) | Removes the element at the specified position in this list. |
3 | boolean remove(Object o) | Removes the first occurrence of the specified element from this list, if it is present. |
4 | E removeFirst() | Removes and returns the first element from this list. |
5 | boolean removeFirstOccurrence(Object o) | Removes the first occurrence of the specified element in this list (when traversing the list from head to tail). |
6 | E removeLast() | Removes and returns the last element from this list. |
7 | boolean removeLastOccurrence(Object o) | Removes the last occurrence of the specified element in this list (when traversing the list from head to tail). |
2. Remove or delete nodes/String objects from LinkedList (java/ example)
package org.learn.collection.list.linkedlist; import java.util.LinkedList; public class DemoRemoveInLinkedList { public static void main(String[] args) { LinkedList<String> linkedList = new LinkedList<>(); linkedList.add("archery"); linkedList.add("badminton"); linkedList.add("canoe"); linkedList.add("boxing"); linkedList.add("canoe"); linkedList.add("diving"); linkedList.add("badminton"); linkedList.add("diving"); System.out.println("Demo of remove or delete methods of LinkedList: "); demoRemoveMethod(linkedList); } private static void demoRemoveMethod(LinkedList<String> linkedList) { // [archery, badminton, canoe, boxing, diving, badminton, diving] System.out.println("Orignal LinkedList:" + linkedList); // Remove element by index String removedElement = linkedList.remove(2); // [archery, badminton, boxing, diving, badminton, diving] System.out.printf("1. Removed element %s from index 2: %s \n", removedElement, linkedList); // Remove element by index removedElement = linkedList.removeFirst(); // [badminton, boxing, diving, badminton, diving] System.out.printf("2. Removed first element %s of linked list:%s \n", removedElement, linkedList); // Remove element by index removedElement = linkedList.removeLast(); // [badminton, boxing, diving, badminton] System.out.printf("3. Removed last element %s of linked list:%s \n", removedElement, linkedList); // Remove element after comparing and remove it linkedList.remove("boxing"); // [badminton, diving, badminton] System.out.println("4. Removed boxing from linkedList: " + linkedList); LinkedList<String> removeElementsList = new LinkedList<>(); removeElementsList.add("diving"); // Remove element by supplying another collection linkedList.removeAll(removeElementsList); // [badminton, badminton] System.out.println("5. Removed collection containing diving: " + linkedList); // Remove element by supplying another collection linkedList.removeLastOccurrence("badminton"); // [badminton, badminton] System.out.println("6. Removed last occurrence of badminton: " + linkedList); // Remove element by supplying another collection linkedList.removeFirstOccurrence("badminton"); // [badminton, badminton] System.out.println("7. Removed first occurrence of badminton: " + linkedList); } }
3. Remove or delete nodes/String objects from LinkedList (java/ example)
Demo of remove or delete methods of LinkedList: Orignal LinkedList:[archery, badminton, canoe, boxing, canoe, diving, badminton, diving] 1. Removed element canoe from index 2: [archery, badminton, boxing, canoe, diving, badminton, diving] 2. Removed first element archery of linked list:[badminton, boxing, canoe, diving, badminton, diving] 3. Removed last element diving of linked list:[badminton, boxing, canoe, diving, badminton] 4. Removed boxing from linkedList: [badminton, canoe, diving, badminton] 5. Removed collection containing diving: [badminton, canoe, badminton] 6. Removed last occurrence of badminton: [badminton, canoe] 7. Removed first occurrence of badminton: [canoe]