Develop a Java application to remove first and last element from a linked list

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




// 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];
}
}
Output LinkedList: [Geek, for, Geeks, 2020, 2021] The last element is removed: 2021 Final LinkedList: [Geek, for, Geeks, 2020] The first element is removed: Geek Final LinkedList: [for, Geeks, 2020]




Article Tags :
Java
Java Programs
Technical Scripter
Java-Collections
java-LinkedList
Technical Scripter 2020
Practice Tags :
Java
Java-Collections
Read Full Article

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.

core java linkedlist util 2012-11-11

removeFirst[]

Remove the first element in the LinkedList. Throws NoSuchElementException if the LinkedList is empty.
String element = fruitList.removeFirst[]; System.out.println["Removed the first element " + element + " => " + fruitList];

removeLast[]

Remove the last element in theLinkedList. ThrowsNoSuchElementExceptionif the LinkedList is empty
element = fruitList.removeLast[]; System.out.println["Removed the last element " + element + " => " + fruitList];

Complete Example

package com.javaguides.collections.linkedlistexamples; import java.util.LinkedList; public class RemoveElementsFromLinkedListExample { public static void main[String[] args] { LinkedList fruitList = new LinkedList < > []; fruitList.add["Apple"]; fruitList.add["banana"]; fruitList.add["mango"]; fruitList.add["Pinaple"]; System.out.println["Initial LinkedList = " + fruitList]; // Remove the first element in the LinkedList String element = fruitList.removeFirst[]; System.out.println["Removed the first element " + element + " => " + fruitList]; // Remove the last element in the LinkedList element = fruitList.removeLast[]; System.out.println["Removed the last element " + element + " => " + fruitList]; } }

Delete first and last element from a LinkedList in Java

Java 8Object Oriented ProgrammingProgramming

The first element can be deleted from a LinkedList by using the method java.util.LinkedList.removeFirst[]. This method does not have any parameters and it returns the first element of the LinkedList.

The last element can be deleted from a LinkedList by using the method java.util.LinkedList.removeLast[]. This method does not have any parameters and it returns the last element of the LinkedList.

A program that demonstrates this is given as follows −

Video liên quan

Bài Viết Liên Quan

Bài mới nhất

Chủ Đề