Open In App

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

Last Updated : 15 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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);
	}
}

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).

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);
    }
}

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).

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);
	}
}

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.

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);
	}
}

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.


Next Article

Similar Reads