VOOZH about

URL: https://www.geeksforgeeks.org/java/java-util-linkedlist-offer-offerfirst-offerlast-java/

⇱ Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java

Last Updated : 15 Apr, 2025

In Java, the LinkedList class is present in java.util package, and it also has different methods that do the work of flexible addition of elements and help addition both at the front and back of the list. In this article, we cover the LinkedList offer(), offerFirst(), and offerLast() methods. These methods are useful when we want to add an element to a specific position of a LinkedList.

1. offer() Method

This method is used to add an element to the last or end part of a LinkedList. This method is also used in the queue interface that is internally used in the LinkedList. This method does not throw any exception, unlike the add() method.

Syntax:

public boolean offer(E e)

  • Parameter: It takes a single parameter e, which is the element added to the list.
  • Return Type: This method returns a Boolean value. If the element is successfully added to our LinkedList, then it returns true; otherwise, it returns false.

Example: Adding an element to the LinkedList using the method offer(E e).


Output
The Linked list is : [3, 5, 7]

Explanation: In the above example, we use the offer() method which add the element in the last of a LinkedList.

2. offerFirst() Method

This method is useful when we want to add the an element in the starting of the LinkedList. Using this method, we can add the element in the first position of the LinkedList and this method is part of Deque interface which allows us to perform operations on both front and back end.

Syntax:

public void offerFirst(E e)

  • Parameter: This is the element which we used to add in the first position of a LinkedList.
  • Return Type: This method does not return anything but it modify the list and add the element in first position of a LinkedList.

Example: Adding an element in the first position of a LinkedList using offerFirst(E e).


Output
The initial Linked list is : [Geeks, 4, Geeks, 8]
LinkedList after insertion using offerFirst() : [Geek1, Geeks, 4, Geeks, 8]

Explanation: In the above example, we use the offerFirst() method to add "Geek1" to the first position of a LinkedList.

3. offerLast() Method

This method is used to add an element in the end of the list or we can say it insert the element in the last position in a LinkedList. It is similar to the add() and offer() method which add the element in the last of a LinkedList.

Syntax:

public void offerLast(E e)

  • Parameter: This method takes a single parameter which added in the last of a LinkedList.
  • Return Type: This method does not return anything but modify the LinkedList and add the element in the last position passes as an argument.

Example: Adding an element in the last position using the method offerLast( E e).


Output
The initial Linked list is : [Geeks, 4, Geeks, 8]
LinkedList after insertion using offerLast() : [Geeks, 4, Geeks, 8, Geek3]

Explanation: In the above example, we use the offerLast() method which adds "Geek3" to the last position of a LinkedList.


Priority Based Sorting Using offerFirst() and offerLast()

Example: Using the offerFirst() and offerLas() methods of LinkedList to perform practical operation on priority addition in queues where elements having a greater number than threshold.


Output
The initial Linked list is : [12, 4, 8, 10, 3, 15]
The prioritized Linked list is : [15, 10, 12, 4, 8, 3]

Explanation: In the above example, we use methods offer(), offerFirst() and offerLast() to perform the operation to add the element on the basis of their priority this is the real life example where we need to use these methods.

Comment