Linked List – offer, offerFirst, and offerLast methods

In this post, we will discuss the offer, offerFirst, and offerLast methods of the Java LinkedList class. The offer methods add elements at the linked list’s start or end.

public boolean offer(E e)

  • What does it do? It will take an element or object as an argument and add it at the end of the linked list.
  • What does it return? It will return true after executing the function successfully.
Code Example
public class Codekru {

	public static void main(String[] args) {

		LinkedList<Object> linkedList = new LinkedList<Object>();

		linkedList.offer("first");
		linkedList.offer("second");

		System.out.println(linkedList);

	}
}

Output –

[first, second]

We have taken a LinkedList object and added the elements to it using the offer method.

Internally, the offer() method used the add() method of the linked list class and was introduced in java 1.5 under the queue interface.

Internal implementation of the offer() method
    public boolean offer(E e) {
        return add(e);
    }
Time complexity of the offer() method

As the offer function uses the add method internally, it will have the same complexity as the add() method. So, the time complexity of the offer() method would also be O(1).

public boolean offerFirst(E e)

It was introduced in java 1.6 under the deque interface.

  • What does it do? It will take an element or object as an argument and add it at the start of the linked list.
  • What does it return? It will return true after executing the function successfully.
import java.util.LinkedList;

public class Codekru {

	public static void main(String[] args) {

		LinkedList<Object> linkedList = new LinkedList<Object>();

		linkedList.offerFirst("first");
		linkedList.offerFirst("second");

		System.out.println(linkedList);

	}
}

Output –

[second, first]

Here, the elements were added at the front of the linked list using the offerFirst() method.

The offerFirst method internally called the addFirst method.

Internal implementation of offerFirst() method
    public boolean offerFirst(E e) {
        addFirst(e);
        return true;
    }
Time complexity of offerFirst() method

As the offerFirst function uses the addFirst method internally, it will have the same complexity as the addFirst() method. So, the time complexity of the offerFirst() method would also be O(1).

public boolean offerLast(E e)

It was also introduced in java 1.6 under deque interface.

  • What does it do? It will take an element or object as an argument and add it at the end of the linked list.
  • What does it return? It will return true after executing it successfully.
import java.util.LinkedList;

public class Codekru {

	public static void main(String[] args) {

		LinkedList<Object> linkedList = new LinkedList<Object>();

		linkedList.offerLast("first");
		linkedList.offerLast("second");

		System.out.println(linkedList);

	}
}

Output –

[first, second]

Internally, the offerLast method calls the addLast method.

Internal implementation of offerLast method
    public boolean offerLast(E e) {
        addLast(e);
        return true;
    }
Time complexity of the offerLast method

offerLast() has the same time complexity as the addLast() method. So, the time complexity of the offerLast() method would be O(1) as well.

Difference between offer() vs. offerLast() methods?

Now, the question is why there are two methods (offer and offerLast) doing essentially the same thing?

  • Well, the offer method came first in java 1.5 under the queue interface, whereas the offerLast method came in java 1.6 under the deque interface. As the linked list class implements both the interfaces, it has to implement both the methods and ends up implementing the methods having the same functionality.
  • offer() method internally uses the add() method whereas the offerLast() method uses the addLast() method.

We recommend reading this article to learn more about the linked list and its methods.

We hope that you find this helpful. Please feel free to write us in the comments or mail us at [email protected] for any doubts or concerns.

Liked the article? Share this on

Leave a Comment

Your email address will not be published.