In Java, remove() has three forms. With no argument it removes and returns the head of the LinkedList. With an int index it removes and returns the element at that position. With an object it removes the first matching element and says whether it found one.
After a removal, the elements after the removed position move up by one, so the list becomes one shorter. A LinkedList does this by relinking two nodes, not by copying elements.
An instance method of java.util.LinkedList (since Java 1.2): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by List, Queue and Collection, and LinkedList implements it.
Syntax
Three forms, chosen by the argument you pass.
Java
LinkedList<String> list = new LinkedList<>();
list.add("A"); list.add("B"); list.add("C"); list.add("B");
String head = list.remove(); // "A" -> [B, C, B]
String at1 = list.remove(1); // "C" -> [B, B]
boolean gone = list.remove("B"); // true -> [B]
Call
What it does
list.remove()
Removes and returns the head. Throws if the list is empty.
list.remove(index)
Removes and returns the element at index.
list.remove(obj)
Removes the first element equal to obj. Returns true if one was removed.
list.removeFirstOccurrence(obj)
The deque name for removing the first match.
The result type depends on the form: an element for the first two, and a boolean for the object form.
Library declarations
Java
// java.util.LinkedList
public E remove()
public E remove(int index)
public boolean remove(Object o)
Parameters
Parameter
Description
index (int)
The position to remove, from 0 to size() - 1.
o (Object)
The element to look for. Compared with equals.
Return Value
Return Value
Description
E
For remove() and remove(index): the removed element.
boolean
For remove(o): true if an element was removed, false if there was no match.