In Java, removeFirst() takes the first element (the head) out of the LinkedList and returns it. The second element becomes the new head.
It takes the same short time on a list of any size, since only the first link has to change. If the list is empty there is nothing to remove, so it throws NoSuchElementException.
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 Deque, and LinkedList implements it. Call it on a LinkedList (or Deque) variable: a plain List variable cannot see it.
Syntax
Call it on a list that is not empty.
Java
LinkedList<String> list = new LinkedList<>();
list.add("A");
list.add("B");
String gone = list.removeFirst(); // "A", list is now [B]
Call
What it does
list.removeFirst()
Removes and returns the head. Throws if empty.
list.pollFirst()
Removes and returns the head, or null if empty.
list.remove()
The queue name for the same action.
list.pop()
The stack name for the same action.
removeFirst, remove() and pop() are three names for one operation.
Example 1 — Serve Customers from the Front of the List
Remove customers from the front one at a time and print who was served.
Java
import java.util.LinkedList;
public class LinkedListRemoveFirstDemo {
public static void main(String[] args) {
LinkedList<String> line = new LinkedList<>();
line.add("Ruth");
line.add("Sam");
line.add("Tara");
String served = line.removeFirst();
System.out.println("served: " + served);
System.out.println("waiting: " + line);
served = line.removeFirst();
System.out.println("served: " + served);
System.out.println("waiting: " + line);
}
}
Output
served: Ruth
waiting: [Sam, Tara]
served: Sam
waiting: [Tara]
How It Works
Three people wait. Ruth arrived first, so she is at the front.
First removal.removeFirst() returns "Ruth" and takes her out of the line.
Second removal. Sam is now the head, so he is served next.
Only Tara is left. The final waiting list holds a single name.
Example 2 — Empty a List with a Loop
Use removeFirst in a loop that stops when the list is empty, and add up the removed values.
Java
import java.util.LinkedList;
public class LinkedListRemoveFirstDrain {
public static void main(String[] args) {
LinkedList<Integer> pile = new LinkedList<>();
for (int i = 1; i <= 4; i++) {
pile.add(i * 5);
}
int total = 0;
while (!pile.isEmpty()) {
int value = pile.removeFirst();
total += value;
System.out.println("took " + value + ", left " + pile.size());
}
System.out.println("total: " + total);
}
}
Output
took 5, left 3
took 10, left 2
took 15, left 1
took 20, left 0
total: 50
How It Works
Four values. The pile holds 5, 10, 15 and 20.
The guard. The loop only runs while isEmpty() is false, so removeFirst() never meets an empty list.
Taken in order. Each pass takes the current head and shows how many items are left.
Total of everything. The values add up to 50, and the pile ends empty.
Example 3 — See removeFirst Fail and pollFirst Return null
Call both methods on an empty list and compare the results.