Java LinkedList offerFirst() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, offerFirst(e) inserts the element at the front of the LinkedList and returns true. It belongs to the Deque interface, which describes a "double-ended queue".

The Deque contract lets a bounded deque answer false when it is full. A LinkedList has no size limit, so it always answers true, and the call acts just like addFirst(e).

An instance method of java.util.LinkedList (since Java 1.6): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by Deque, and LinkedList implements it. Call it on a LinkedList (or Deque) variable: a plain List variable cannot see it.

Syntax

Pass the element for the front position.

Java
LinkedList<String> list = new LinkedList<>();
boolean a = list.offerFirst("B");   // true, [B]
boolean b = list.offerFirst("A");   // true, [A, B]
CallWhat it does
list.offerFirst(e)Inserts e at the head and returns true.
list.addFirst(e)Same effect, with no return value.
list.push(e)Same effect, under the stack name.
list.offerLast(e)Inserts at the tail instead.

The result is always true for a LinkedList. Use it when your code is written against the Deque interface and must also work with bounded deques.

Library declarations
Java
// java.util.LinkedList
public boolean offerFirst(E e)

Parameters

ParameterDescription
e (E)The element to insert at the front. It may be null.

Return Value

Return ValueDescription
booleantrue, always, for a LinkedList.

Compare with Similar Methods

Ways to insert at the front:

MethodHow it differs
addFirst(e)Same action without a return value.
push(e)Same action, named for stack use.
offerLast(e)The matching call for the tail.
ArrayDeque.offerFirst()The array-based deque version. It rejects null.

Java Examples

Example 1 — Offer Items to the Front and Read the Result

Offer two names to the front of a deque and print the results.

Java
import java.util.Deque;
import java.util.LinkedList;

public class LinkedListOfferFirstDemo {
    public static void main(String[] args) {
        Deque<String> deque = new LinkedList<>();

        boolean x = deque.offerFirst("Mia");
        boolean y = deque.offerFirst("Leo");

        System.out.println("deque: " + deque);
        System.out.println("results: " + x + " " + y);
        System.out.println("front: " + deque.peekFirst());
    }
}

How It Works

  1. A Deque variable. The list is used through the Deque interface, which provides offerFirst.
  2. Each offer goes to the front. "Mia" goes in first, then "Leo" is placed in front of her.
  3. True both times. The deque has no size limit, so both calls report success.
  4. peekFirst shows the head. The front element is "Leo", the one offered last.

Example 2 — Let Urgent Tickets Jump to the Front

Add normal tickets to the back and urgent tickets to the front of the same line.

Java
import java.util.Deque;
import java.util.LinkedList;

public class LinkedListOfferFirstUrgent {
    public static void main(String[] args) {
        Deque<String> line = new LinkedList<>();
        line.offerLast("T-1 normal");
        line.offerLast("T-2 normal");
        System.out.println("start:  " + line);

        line.offerFirst("T-3 URGENT");
        System.out.println("urgent: " + line);

        line.offerLast("T-4 normal");
        line.offerFirst("T-5 URGENT");
        System.out.println("final:  " + line);
    }
}

How It Works

  1. Normal tickets queue at the back. offerLast keeps T-1 and T-2 in arrival order.
  2. An urgent ticket jumps ahead. offerFirst places T-3 in front of both normal tickets.
  3. Later arrivals keep the pattern. T-4 goes to the back, and T-5 goes to the front.
  4. The newest urgent ticket leads. T-5 ends up ahead of T-3, because each offerFirst takes the very first position.

Example 3 — Check That offerFirst and addFirst Agree

Insert the same values with offerFirst and addFirst, and compare the lists.

Java
import java.util.LinkedList;

public class LinkedListOfferFirstSame {
    public static void main(String[] args) {
        LinkedList<Character> viaOffer = new LinkedList<>();
        LinkedList<Character> viaAdd = new LinkedList<>();

        for (char c = 'a'; c <= 'd'; c++) {
            viaOffer.offerFirst(c);
            viaAdd.addFirst(c);
        }

        System.out.println("offerFirst: " + viaOffer);
        System.out.println("addFirst:   " + viaAdd);
        System.out.println("equal: " + viaOffer.equals(viaAdd));
    }
}

How It Works

  1. Letters a to d. The loop walks through four characters and gives each one to both lists.
  2. Reverse order in both. Every new letter goes to the front, so the lists read d, c, b, a.
  3. Identical results. The two lists contain exactly the same sequence.
  4. That is why equals is true. The only difference between the methods is that offerFirst also returns true.

Frequently Asked Questions

It inserts the element at the front (head) of the list and returns true.
The effect on a LinkedList is the same. offerFirst returns true, and addFirst returns nothing. For bounded deques, offerFirst returns false when full while addFirst throws an exception.
No. A LinkedList is never full, so the result is always true.
No. It is declared by the Deque interface, so the variable must be a Deque or a LinkedList.
A LinkedList accepts null. An ArrayDeque does not, and throws a NullPointerException.

Next: offerLast()

Insert an element at the tail and return true.

offerLast() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful