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

We will discuss the Java LinkedList class and its functions, making our work flexible while writing the code. A linked list is one of the major concepts of data structures to prepare for interview rounds. We will discuss the syntax and examples of LinkedList offer in java: offer, offerFirst, and offerLast methods of the Java LinkedList class.

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 an 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 to LinkedList offer in java

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 of LinkedList offer in java: O(1), as the new element gets added to the end of the list.

Space Complexity of LinkedList offer in java: 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 of LinkedList offer in java: 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 of LinkedList offer in java

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 of LinkedList offer in java: O(1), as the new element gets added to the end of the list.

Space Complexity of LinkedList offer in java: 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 of LinkedList offer in java

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]

Conclusion

In this article, we learn about the LinkedList offer in java and 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 is curated by our expert mentors at PrepBytes, you can follow this link Linked List.

FAQs related to LinkedList offer in java

1. What do you mean by a linked list in data structures?
LinkedList is a linear data structure where elements are connected through links in non-contiguous locations. Each element is known as a node. There are three types of LinkedList known as Singly LinkedList, Doubly LinkedList, and Circular LinkedList.

2. What are the three additional methods of the java LinkedList class?
Three functions are offer, offerFirst(), and offerLast(), which help elements get inserted at the front and rear end of the list.

Leave a Reply

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