Java LinkedList removeFirst() Method
removeFirst()is a method in JavaLinkedLista method provided by the class, used to remove and return the first element of the linked list. This method belongs tojava.util.LinkedListthe class, and is one of the basic operations of the doubly linked list data structure.
Method Syntax
public E removeFirst()
Parameters
This methoddoes not requireany parameters.
Return Value
- Returns the first element of the linked list that was removed
- The return type is generic E (that is, the type specified when LinkedList was declared)
Method Behavior
Normal Cases
When the linked list is not empty:
- The method removes the first element in the linked list
- Returns the removed element
- The original second element becomes the new first element
- The size of the linked list decreases by 1
Exception Cases
When the linked list is empty (size = 0), calling this method throwsNoSuchElementExceptionan exception.
Usage Examples
Basic Example
Example
public class LinkedListDemo {
public static void main(String[] args) {
// Create a LinkedList
LinkedList<String> fruits = new LinkedList<>();
// Add elements
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
System.out.println("Original LinkedList: " + fruits); // Output: [Apple, Banana, Cherry]
// Use the removeFirst() method
String firstFruit = fruits.removeFirst();
System.out.println("Removed element: " + firstFruit); // Output: Apple
System.out.println("LinkedList after operation: " + fruits); // Output: [Banana, Cherry]
}
}
Handling Empty LinkedList
Example
import java.util.NoSuchElementException;
public class EmptyLinkedListDemo {
public static void main(String[] args) {
LinkedList<Integer> numbers = new LinkedList<>();
try {
int first = numbers.removeFirst();
System.out.println("Removed element: " + first);
} catch (NoSuchElementException e) {
System.out.println("The linked list is empty, cannot remove an element!");
}
}
}
Comparison of Related Methods
| Method Name | Description | Behavior When LinkedList Is Empty |
|---|---|---|
removeFirst() |
Removes and returns the first element | Throws NoSuchElementException |
pollFirst() |
Removes and returns the first element | Returns null |
pop() |
Removes and returns the first element (stack operation) | Throws NoSuchElementException |
remove() |
Removes and returns the first element | Throws NoSuchElementException |
Practical Application Scenarios
- Queue processing: When using LinkedList as a queue, removeFirst() can be used to implement FIFO (First-In, First-Out) operations
- Undo operations: In some applications, it may be necessary to remove the most recently added operation records
- Task scheduling: Process the first task in the task list
Performance Considerations
removeFirst()The time complexity of the method is O(1), because:
- LinkedList internally maintains head and tail pointers
- Removing the first element only requires adjusting the head pointer and references of adjacent nodes
- No need to shift elements like in an array
Best Practices
- Empty linked list check: Before using removeFirst(), it is best to check whether the linked list is empty
- Alternative method: If you don't want to handle exceptions, you can consider using the pollFirst() method
- Pairing with addFirst(): Often used with the addFirst() method to implement a stack structure
Example
stack.addFirst("First"); // Push
stack.addFirst("Second"); // Push
String top = stack.removeFirst(); // Pop
Summary
LinkedList.removeFirst()is an efficient method for removing and returning the first element of a linked list. Understanding the behavior and exception cases of this method is very important for correctly using LinkedList. In actual development, depending on whether you need to handle empty linked list cases, you can choose to use removeFirst() or pollFirst() method.
Java LinkedList