This example shows how to remove the last element of the LinkedList in Java. This example also shows removing the last element of the linked list using a couple of methods.
How to remove last element of the LinkedList in Java?
There are a couple of ways using which you can remove the last element of the LinkedList object in Java.
1. Using the removeLast method
You can use the removeLast
method of the LinkedList class to remove the last element.
1 |
public E removeLast() |
The removeLast
method removes the last element of the linked list and returns it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.LinkedList; public class LinkedListRemoveLastElementExample { public static void main(String[] args) { //create new LinkedList object LinkedList<String> linkedListColors = new LinkedList<String>(); //add elements linkedListColors.add("Red"); linkedListColors.add("Green"); linkedListColors.add("Blue"); System.out.println("LinkedList contains: " + linkedListColors); /* * To remove last element of the LinkedList, use * the removeLast method. * * It returns the element that was removed from the list */ String str = linkedListColors.removeLast(); System.out.println(str + " element is removed"); System.out.println("LinkedList contains: " + linkedListColors); } } |
Output
1 2 3 |
LinkedList contains: [Red, Green, Blue] Blue element is removed LinkedList contains: [Red, Green] |
2. Using the remove method
You can also use the remove
method of the LinkedList class.
1 |
public E remove(int index) |
The remove
method deletes the element at the specified index from the list.
Since the linked list in Java starts from index 0, to delete the last element, pass the LinkedList size – 1 as the index in the remove
method as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.util.LinkedList; public class LinkedListRemoveLastElementExample { public static void main(String[] args) { //create new LinkedList object LinkedList<String> linkedListColors = new LinkedList<String>(); //add elements linkedListColors.add("Red"); linkedListColors.add("Green"); linkedListColors.add("Blue"); System.out.println("LinkedList contains: " + linkedListColors); /* * Always check the linked list size first */ if( linkedListColors.size() > 0 ){ //pass list lenght - 1 in the remove method String str = linkedListColors.remove( linkedListColors.size() - 1 ) ; System.out.println(str + " element is removed"); } System.out.println("LinkedList contains: " + linkedListColors); } } |
Output
1 2 3 |
LinkedList contains: [Red, Green, Blue] Blue element is removed LinkedList contains: [Red, Green] |
The remove
method of the linked list size throws IndexOutOfBoundsException if the specified index is out of the range. That is if the specified index is less than zero or greater than or equal to the size of the linked list object.
In order to avoid this exception, it is necessary to check the linked list size before removing the last element using the remove
method.
This example is a part of the LinkedList in Java tutorial.
Please let me know your views in the comments section below.