🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this article, we will discuss how to remove the first and last elements of a LinkedList in Java.
Table of Contents
- Introduction
- Removing First Element
- Removing Last Element
- Complete Example
- Conclusion
Introduction
A LinkedList in Java is a part of the Java Collections Framework and implements the List and Deque interfaces. This allows it to perform various operations efficiently, including removing elements from both ends of the list. The LinkedList class provides several methods to remove the first and last elements.
Removing First Element
To remove the first element from a LinkedList, you can use the removeFirst() or pollFirst() methods. Both methods remove and return the first element of the list.
Example
import java.util.LinkedList;
public class RemoveFirstElement {
public static void main(String[] args) {
LinkedList<String> fruits = new LinkedList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
// Removing the first element using removeFirst()
String firstElement = fruits.removeFirst();
System.out.println("Removed First Element: " + firstElement);
System.out.println("LinkedList after removing first element: " + fruits);
// Adding elements back for next example
fruits.addFirst("Apple");
// Removing the first element using pollFirst()
firstElement = fruits.pollFirst();
System.out.println("Removed First Element using pollFirst(): " + firstElement);
System.out.println("LinkedList after removing first element using pollFirst(): " + fruits);
}
}
Output:
Removed First Element: Apple
LinkedList after removing first element: [Banana, Orange]
Removed First Element using pollFirst(): Apple
LinkedList after removing first element using pollFirst(): [Banana, Orange]
Removing Last Element
To remove the last element from a LinkedList, you can use the removeLast() or pollLast() methods. Both methods remove and return the last element of the list.
Example
import java.util.LinkedList;
public class RemoveLastElement {
public static void main(String[] args) {
LinkedList<String> fruits = new LinkedList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
// Removing the last element using removeLast()
String lastElement = fruits.removeLast();
System.out.println("Removed Last Element: " + lastElement);
System.out.println("LinkedList after removing last element: " + fruits);
// Adding elements back for next example
fruits.addLast("Orange");
// Removing the last element using pollLast()
lastElement = fruits.pollLast();
System.out.println("Removed Last Element using pollLast(): " + lastElement);
System.out.println("LinkedList after removing last element using pollLast(): " + fruits);
}
}
Output:
Removed Last Element: Orange
LinkedList after removing last element: [Apple, Banana]
Removed Last Element using pollLast(): Orange
LinkedList after removing last element using pollLast(): [Apple, Banana]
Complete Example
Here's a complete example that demonstrates both the removal of the first and last elements from a LinkedList.
import java.util.LinkedList;
public class RemoveFirstAndLastElements {
public static void main(String[] args) {
LinkedList<String> fruits = new LinkedList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
// Removing the first element
String firstElement = fruits.removeFirst();
System.out.println("Removed First Element: " + firstElement);
System.out.println("LinkedList after removing first element: " + fruits);
// Adding elements back
fruits.addFirst("Apple");
// Removing the first element using pollFirst()
firstElement = fruits.pollFirst();
System.out.println("Removed First Element using pollFirst(): " + firstElement);
System.out.println("LinkedList after removing first element using pollFirst(): " + fruits);
// Removing the last element
String lastElement = fruits.removeLast();
System.out.println("Removed Last Element: " + lastElement);
System.out.println("LinkedList after removing last element: " + fruits);
// Adding elements back
fruits.addLast("Orange");
// Removing the last element using pollLast()
lastElement = fruits.pollLast();
System.out.println("Removed Last Element using pollLast(): " + lastElement);
System.out.println("LinkedList after removing last element using pollLast(): " + fruits);
}
}
Output:
Removed First Element: Apple
LinkedList after removing first element: [Banana, Orange]
Removed First Element using pollFirst(): Apple
LinkedList after removing first element using pollFirst(): [Banana, Orange]
Removed Last Element: Orange
LinkedList after removing last element: [Apple, Banana]
Removed Last Element using pollLast(): Orange
LinkedList after removing last element using pollLast(): [Apple, Banana]
Conclusion
Removing the first and last elements of a LinkedList in Java can be done efficiently using the removeFirst(), pollFirst(), removeLast(), and pollLast() methods. These methods provide a convenient way to manage elements at both ends of the list. This guide provided examples to demonstrate the usage of these methods in various scenarios.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment