Remove First and Last Elements from LinkedList in Java
The Java.util.LinkedList.removeFirst[] method is used to remove the first element from the LinkedList.
The Java.util.LinkedList.removeLast[] method is used to remove the last element from the LinkedList. Both the methods also returns the element after removing it.
1. removeFirst[]
Syntax:
LinkedList.remove[]Parameters: This function does not take any parameters.
Return Value: The method returns the first element or the element present at the head of the list.
2. removeLast[]
Syntax:
LinkedList.removeLast[]Parameters: This function does not take any parameters.
Return Value: The method returns the last element or the element present at the tail of the list.
Example:
// Java program to illustrate the Java.util.LinkedList.remove[] method
// and Java.util.LinkedList.removeLast[] method
import java.util.LinkedList;
class GFG {
public static void main [String[] args] {
// Creating an LinkedList
LinkedList list = new LinkedList[];
// Use add[] method to add elements in the list
list.add["Geek"];
list.add["for"];
list.add["Geeks"];
list.add["2020"];
list.add["2021"];
// Displaying the list
System.out.println["LinkedList:\t" + list];
// Remove the tail using removeLast[]
System.out.println["The last element is removed:\t" + list.removeLast[]];
// Displaying the final list
System.out.println["Final LinkedList:\t" + list];
// Remove the head using remove[]
System.out.println["The first element is removed:\t" + list.removeFirst[]];
// Displaying the final list
System.out.println["Final LinkedList:\t" + list];
}
}
|
Java Collection, LinkedList Exercises: Remove first and last element from a linked list
Last update on December 14 2021 10:52:29 [UTC/GMT +8 hours]Remove first and last element from LinkedList example
Posted by: Ilias Tsagklis in LinkedList November 11th, 2012 0
In this example we shall show you how to remove the first and the last element of a LinkedList. To remove the first and the last element of a LinkedList one should perform the following steps:
- Create a new LinkedList.
- Populate the list with elements, with the add[E e] API method of the LinkedLink.
- Invoke the removeFirst[] API method of the LinkedList. It removes and returns the first element of the list. Subsequent elements are shifted the to the left.
- Invoke the removeLast[] API method of the LinkedList. It removes and returns
the last element of the list,
as described in the code snippet below.
package com.javacodegeeks.snippets.core; import java.util.LinkedList; public class RemoveFirstLastElementLinkedList { public static void main[String[] args] { // Create a LinkedList and populate it with elements LinkedList linkedList = new LinkedList[]; linkedList.add["element_1"]; linkedList.add["element_2"]; linkedList.add["element_3"]; linkedList.add["element_4"]; linkedList.add["element_5"]; System.out.println["LinkedList contains : " + linkedList]; /* * Object removeFirst[] operation removes and returns the first * element of LinkedList. Subsequent elements are shifted the to * the left */ Object firstElement = linkedList.removeFirst[]; System.out.println[firstElement + " has been removed from LinkedList"]; System.out.println["LinkedList contains : " + linkedList]; /* * Object removeLast[] operation removes and returns the last element * of LinkedList */ Object lastElement = linkedList.removeLast[]; System.out.println[lastElement + " has been removed from LinkedList"]; System.out.println["LinkedList contains : " + linkedList]; } }Output:
LinkedList contains : [element_1, element_2, element_3, element_4, element_5] element_1 has been removed from LinkedList LinkedList contains : [element_2, element_3, element_4, element_5] element_5 has been removed from LinkedList LinkedList contains : [element_2, element_3, element_4]
This was an example of how to remove the first and the last element of a LinkedList in Java.