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

Introduction

One of the most crucial data structures to learn while preparing for interviews is the linked list. In a coding interview, having a thorough understanding of Linked Lists might be a major benefit.

The linked list additionally contains a function that handles the task of flexible element addition and helps addition both at the front and rear of the list ().
These functions actually offer the facility and are termed as offer().

offer(Element x) Method

Syntax: public boolean offer (Element x)

Parameters: x, the element to be added.

Return Value: This function returns true after successful execution.

This function adds the given element to the end of the list, i.e., that element becomes the tail of the list. We will just call the offer(Element x) function.

Input
[Coding, is], Element to be added: Fun

Output
[Coding, is, Fun]

Explanation: As we can see, “Fun” was added as the tail of the list.

Code Implementation

import java.util.*;
public class PrepBytes {
  
public static void main(String[] args)
    {

        LinkedList list = new LinkedList();

        list.add("Coding");
        list.add("is");

        System.out.println("The initial Linked list is : " + list);

        list.offer("Fun");

        System.out.println("LinkedList after insertion using offer() : " + list);
    }
}
Output

The initial Linked list is : [Coding, is]
LinkedList after insertion using offer() : [Coding, is, Fun]

Time Complexity: O(1), as the new element gets added to the end of the list.

Space Complexity: O(1)

offerFirst(Element x) Method

Syntax: public boolean offerFirst (Element x)

Parameters: x, the element to be added.

Return Value: This function returns true after successful execution.

This function adds the given element to the start of the list, i.e., that element becomes the head of the list. We will just call the offerFirst(Element x) function.

Input
[Coding, is], Element to be added - Fun

Output
[Fun, Coding, is]

Explanation: As we can see, Fun was added as the head of the list.

Code Implementation

import java.util.*;
public class PrepBytes {
  
public static void main(String[] args)
    {

        LinkedList list = new LinkedList();

        list.add("Coding");
        list.add("is");

        System.out.println("The initial Linked list is : " + list);

        list.offerFirst("Fun");

        System.out.println("LinkedList after insertion using offerFirst() : " + list);
    }
}
Output

The initial Linked list is : [Coding, is]
LinkedList after insertion using offerFirst() : [Fun, Coding, is]

Time Complexity: O(1), as the new element gets added at the front of the list.

Space Complexity: O(1)

offerLast(Element x) Method

Syntax: public boolean offerLast (Element x)

Parameters: x, the element to be added.

Return Value: This function returns true after successful execution.

This function adds the given element to the end of the list, i.e. that element becomes the tail of the list. We will just call the offerLast(Element x) function.

Input
[Coding, is], Element to be added - Fun

Output
[Coding, is, Fun]

Explanation: As we can see, “Fun” was added as the tail of the list.

Code Implementation

import java.util.*;
public class PrepBytes {
  
public static void main(String[] args)
    {

        LinkedList list = new LinkedList();

        list.add("Coding");
        list.add("is");

        System.out.println("The initial Linked list is : " + list);

        list.offerLast("Fun");

        System.out.println("LinkedList after insertion using offerLast() : " + list);
    }
}
Output

The initial Linked list is : [Coding, is]
LinkedList after insertion using offerLast() : [Coding, is, Fun]

Time Complexity: O(1), as the new element gets added to the end of the list.

Space Complexity: O(1)

Practical Application

This flexible addition characteristic of these functions can be used in priority addition in queues where items with a larger number than the threshold must be handled before those with a lower number. This is discussed in the code below.

In the below code, we have two lists. One contains all the elements and the other one is empty. All the elements of the first list that have a value greater than the threshold will get added to the front of the second list using offerFirst(), and the elements that have a value lesser than the threshold will get added to the end of the second list using offerLast().

Input

List 1: [15, 19, 8, 111, 3, 12]
List 2: Empty
Threshold = 13

Output

List 1: [15, 19, 8, 111, 3, 12]
List 2: [12, 111, 19, 15, 8, 3]

Explanation: As we can see, all the elements of list 1 that were greater than the threshold (13), were added to the front of list 2 and the elements of list 1 that were lesser than the threshold (13) were added to the end of the list 2.

Code Implementation

import java.util.*;
public class PrepBytes {
  
public static void main(String[] args)
    {
 
        LinkedList list = new LinkedList();
        LinkedList prioList = new LinkedList();
 
        list.add(15);
        list.add(19);
        list.add(8);
        list.add(111);
        list.add(3);
        list.add(12);
 
        int thres = 11;
 
        System.out.println("The initial Linked list is : " + list);
  
        while (!list.isEmpty()) {
  
            int t = list.poll();
 
            if (t >= 11)
                prioList.offerFirst(t);
            else
                prioList.offerLast(t);
        }
 
        System.out.println("The prioritized Linked list is : " + prioList);
    }
}

Output

The initial Linked list is : [15, 19, 8, 111, 3, 12]
The prioritized Linked list is : [12, 111, 19, 15, 8, 3]

So, in this article, we have tried to explain how to use offer(), offerFirst() and offerLast() functions in Java. Java Collection Framework is very important when it comes to coding interviews. If you want to solve more questions on Linked List, which are curated by our expert mentors at PrepBytes, you can follow this link Linked List.

Previous post LinkedList descendingIterator in Java with Examples
Next post Modify contents of Linked List

Leave a Reply

Your email address will not be published. Required fields are marked *