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).
Java // Java Program to demonstrate the application // of offer() in linked list import java.util.*; public class Geeks { public static void main(String[] args) { // Declaring LinkedLists LinkedList<Integer> list = new LinkedList<Integer>(); // adding elements list.add(3); list.add(5); // Adding Element in the Last using // offer method list.offer(7); // The resultant list is System.out.println("The Linked list is : " + list); } }
OutputThe 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).
Java // Java program to demonstrate the working // of offerFirst(E e) in linked list import java.util.*; public class Geeks { public static void main(String[] args) { // Declaring a LinkedList LinkedList list = new LinkedList(); // adding elements list.add("Geeks"); list.add(4); list.add("Geeks"); list.add(8); // printing the list System.out.println("The initial Linked list is : " + list); // offering a new element // adds element at head. list.offerFirst("Geek1"); // printing the new list System.out.println("LinkedList after insertion using offerFirst() : " + list); } }
OutputThe 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).
Java // Java code to demonstrate the working // of offerLast(E e) in linked list import java.util.*; public class Geeks { public static void main(String[] args) { // Declaring a LinkedList LinkedList list = new LinkedList(); // adding elements list.add("Geeks"); list.add(4); list.add("Geeks"); list.add(8); // printing the list System.out.println("The initial Linked list is : " + list); // offering a new element // adds element at end. list.offerLast("Geek3"); // printing the new list System.out.println("LinkedList after insertion using offerLast() : " + list); } }
OutputThe 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.
Java // Java program to demonstrate the application // of offer() in linked list import java.util.*; public class Geeks { public static void main(String[] args) { // Declaring LinkedLists LinkedList<Integer> list = new LinkedList<Integer>(); LinkedList prioList = new LinkedList(); // adding elements list.add(12); list.add(4); list.add(8); list.add(10); list.add(3); list.add(15); // declaring threshold int thres = 10; // printing the list System.out.println("The initial Linked list is : " + list); while (!list.isEmpty()) { int t = list.poll(); // adding >=10 numbers at front rest at back if (t >= 10) prioList.offerFirst(t); else prioList.offerLast(t); } System.out.println("The prioritized Linked list is : " + prioList); } }
OutputThe 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.
Similar Reads
LinkedList addAll() Method in Java
In Java, the addAll() method of the LinkedList class is used to add all the elements of one collection to another. This method takes a Collection as an argument and adds all its elements to the end of the list. Example: Here, we use the addAll() method to add all the elements of one collection to an
3 min read
LinkedList addFirst() Method in Java
In Java, the addFirst() method of LinkedList, adds elements at the beginning of the list. All the existing elements moved one position to the right and the new element is placed at the beginning. Syntax of LinkedList addFirst() Method public void addFirst( E e) Parameters: E is the data type of elem
1 min read
LinkedList addLast() Method in Java
In Java, the addLast() method of the LinkedList class is used to add an element at the end of the list. Syntax of LinkedList addLast() Method void addLast( E e) Parameter: e is the element you want to add at the end of the list.Return type: This method does not return any value.Example: Here, we use
1 min read
LinkedList clear() Method in Java
In Java, the clear() is used to remove all the elements from a LinkedList. This method only clears all the element from the list and not deletes the list. After calling this method, the list will be empty. Syntax of LinkedList clear() Methodvoid clear() Parameters: This method does not accept any pa
1 min read
LinkedList clone() Method in Java
In Java, the clone() method of LinkedList, creates a shallow copy which means the structure is duplicated but the objects inside the list are shared between the original and the copied list. So, the cloned list will have the same elements as the original list. Syntax of Java LinkedList clone() Metho
1 min read
LinkedList contains() Method in Java
In Java, the contains() method of LinkedList is used to check whether an element is present in a LinkedList or not. It takes the element as a parameter and returns True if the element is present in the list. Syntax of Java LinkedList contains() Method boolean contains(Object element);Parameter: The
2 min read
LinkedList descendingIterator() Method in Java
In Java, the descendingIterator() method of LinkedList is used to return an iterator that allows to traverse the list in reverse order. Example 1: This example demonstrates how to use descendingIterator() method with Strings in a LinkedList. [GFGTABS] Java // Java program to use // descendingIterato
3 min read
LinkedList element() Method in Java
In Java, the element() method of the LinkedList class is used to retrieve the first element in the list without removing it. The first element of the LinkedList is known as the head. Example 1: Here, we use the element() method to retrieve the first element of the LinkedList of Integers, without rem
2 min read
Java.util.LinkedList.get(), getFirst(), getLast() in Java
In Java, the LinkedList class provides several methods for accessing the elements in the list. Here are the methods for getting the elements of a LinkedList: get(int index): This method returns the element at the specified position in the LinkedList. The index parameter is zero-based, so the first e
5 min read
LinkedList getLast() Method in Java
In Java, the getLast() method in the LinkedList class is used to retrieve the last element of the list. Example 1: Here, we use the getLast() method to retrieve the last element of the list. [GFGTABS] Java // Java Program to Demonstrate the // use of getLast() in LinkedList import java.util.LinkedLi
2 min read