Remove first and last element from LinkedList in Java

In this tutorial, we will learn how to Remove first and last element from LinkedList in Java.

LinkedList in Java is doubly-linked list implementation of the List and Deque interfaces. LinkedList class extends AbstractSequentialList and implements the List, Deque, and Queue interfaces and permits null elements. It provides a linked-list data structure.

If we want to remove first and last element from LinkedList in Java, then this class provides two methods removeFirst() and removeLast() to remove first and last element from LinkedList. We will use these method to remove first and last element from LinkedList. The syntax of removeFirst() and removeLast() methods is:

Syntax of removeFirst() method:

Returns:
the first element from this list

Throws:
NoSuchElementException – if this list is empty

Syntax of removeLast() method:

Returns:
the last element from this list

Throws:
NoSuchElementException – if this list is empty

Example:

Output:

 

Related posts

Leave a Comment