Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
Introduction
We will discuss the Java LinkedList class and its functions, making our work more flexible while writing the code. LinkedList is one of the crucial data structures to prepare for interview rounds.
LinkedList class of the Java collections provides the linked list data structure. Elements in LinkedList are connected through links in a scattered(not in sequence) way. LinkedList offers various methods that allow us to perform different operations in linked lists. It additionally contains a function that helps in addition both at the front and rear end of the list.
We will discuss the syntax and examples of functions: offer, offerFirst, and offerLast methods of the Java LinkedList class.
offer(Element x)
Function: This method adds the element at the end of the list as a tail of the list.
Syntax: public boolean offer(Element x)
Parameter: x - the element to be added
Return value: boolean function so it will return true.
Example of this method with the help of a code
Code
// of offer(Element x) in linked list
import java.util.*;
public class Solution {
public static void main(String[] args){
// Declaring a LinkedList
LinkedList list = new LinkedList();
// adding elements
list.add("Coding");
list.add("Ninjas");
// printing the list
System.out.println("The initial Linked list is : " + list);
//adds the element at the tail
list.offer("CodeStudio");
//after using the method
System.out.println("LinkedList after insertion using offer() : " + list);
}
}
Output
The initial Linked list is : [Coding, Ninjas] LinkedList after insertion using offer() : [Coding, Ninjas, CodeStudio] |
Time Complexity: O(1)
Space Complexity: O(1)
offerFirst(Element x)
Function: This method adds the element at the front of this list.
Syntax: public boolean offerFirst(Element x)
Parameter: x - the element to be added
Return value: boolean function so it will return true.
Example of this method with the help of a code
Code
// Demonstration of offerFirst(Element x) in linked list
import java.util.*;
public class Solution {
public static void main(String[] args){
// Declaring a LinkedList
LinkedList list = new LinkedList();
// adding elements
list.add("Coding");
list.add("Ninjas");
// printing the list
System.out.println("The initial Linked list is : " + list);
//adds at the element at the head of the list
list.offerFirst("CodeStudio");
System.out.println("LinkedList after insertion using offerFirst() : " + list);
}
}
Output
The initial Linked list is : [Coding, Ninjas] LinkedList after insertion using offerFirst() : [CodeStudio, Coding, Ninjas] |
Time Complexity: O(1)
Space Complexity: O(1)
offerLast(Element x)
Function: This method adds the element at the end of this list.
Syntax: public boolean offerLast(Element x)
Parameter: x - the element to be added
Return value: boolean function so it will return true.
Example of this method with the help of the code.
Code
// Java code to demonstrate the working
// Demonstration of offerLast(Element x) in linked list
import java.util.*;
public class Solution {
public static void main(String[] args){
// Declaring a LinkedList
LinkedList list = new LinkedList();
// adding elements
list.add("Coding");
list.add("Ninjas");
// printing the list
System.out.println("The initial Linked list is : " + list);
// offering a new element
// adds element at end.
list.offerLast("CodeStudio");
// printing the new list
System.out.println("LinkedList after using offerLast() : " + list);
}
}
Output
The initial Linked list is : [Coding, Ninjas] LinkedList after using offerLast() : [Coding, Ninjas, CodeStudio] |
Time Complexity: O(1)
Space Complexity: O(1)
Practical Application
The flexible addition of these functions can be done in priority addition in queues where items with a larger number than the threshold must be handled before those with a lower number.
An example of this is discussed in the code below for better understanding.
Code
// Demonstration of the application
// of offer() in linked list
import java.util.*;
public class Solution {
public static void main(String[] args){
// Declaring LinkedLists
LinkedList<Integer> list = new LinkedList<Integer>();
LinkedList list2 = new LinkedList();
// adding elements
list.add(12);
list.add(4);
list.add(8);
list.add(15);
// declaring threshold
int thres = 8;
// printing the list without using the function
System.out.println("The initial LinkedList is : " + list);
while (!list.isEmpty()) {
int t = list.poll();
if (t >= 8)
list2.offerFirst(t);
else
list2.offerLast(t);
}
// The Final list is
System.out.println("The prioritized LinkedList is : " + list2);
}
}
Output
The initial LinkedList is : [12, 4, 8, 15] The prioritized LinkedList is : [15, 8, 12, 4] |
FAQ’s
1. What do you mean by LinkedList data structure?
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 helps elements get inserted at the front and rear end of the list.
Key Takeaways
In this blog, we learn about the additional methods of the Java LinkedList class that helps us to add the specified elements at specified positions. We understand their syntax and function with the help of examples.
You can also have a view of the Data Structures and Algorithms-guided path to start your preparation from scratch.
Happy Coding!!!