Remove First and Last Elements of Linkedlist in Java

In this article, we will discuss how to remove the first and last elements of a LinkedList in Java. 

Table of Contents

  1. Introduction
  2. Removing First Element
  3. Removing Last Element
  4. Complete Example
  5. 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.

Comments