Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
Next article icon

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

Last Updated : 15 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, the LinkedList class is present in java.util package, and it also has different methods that do the work of flexible addition of elements and help addition both at the front and back of the list. In this article, we cover the LinkedList offer(), offerFirst(), and offerLast() methods. These methods are useful when we want to add an element to a specific position of a LinkedList.

1. offer() Method

This method is used to add an element to the last or end part of a LinkedList. This method is also used in the queue interface that is internally used in the LinkedList. This method does not throw any exception, unlike the add() method.

Syntax:

public boolean offer(E e)

  • Parameter: It takes a single parameter e, which is the element added to the list.
  • Return Type: This method returns a Boolean value. If the element is successfully added to our LinkedList, then it returns true; otherwise, it returns false.

Example: Adding an element to the LinkedList using the method offer(E e).

Java
// Java Program to demonstrate the application // of offer() in linked list import java.util.*; public class Geeks {  public static void main(String[] args) 	{ 		// Declaring LinkedLists 		LinkedList<Integer> list = new LinkedList<Integer>(); 	 		// adding elements 		list.add(3); 		list.add(5); 		 		// Adding Element in the Last using 		// offer method 		list.offer(7);  		// The resultant list is 		System.out.println("The Linked list is : " + list); 	} } 

Output
The Linked list is : [3, 5, 7] 

Explanation: In the above example, we use the offer() method which add the element in the last of a LinkedList.

2. offerFirst() Method

This method is useful when we want to add the an element in the starting of the LinkedList. Using this method, we can add the element in the first position of the LinkedList and this method is part of Deque interface which allows us to perform operations on both front and back end.

Syntax:

public void offerFirst(E e)

  • Parameter: This is the element which we used to add in the first position of a LinkedList.
  • Return Type: This method does not return anything but it modify the list and add the element in first position of a LinkedList.

Example: Adding an element in the first position of a LinkedList using offerFirst(E e).

Java
// Java program to demonstrate the working // of offerFirst(E e) in linked list import java.util.*; public class Geeks {  public static void main(String[] args)     {         // Declaring a LinkedList         LinkedList list = new LinkedList();          // adding elements         list.add("Geeks");         list.add(4);         list.add("Geeks");         list.add(8);          // printing the list         System.out.println("The initial Linked list is : " + list);          // offering a new element         // adds element at head.         list.offerFirst("Geek1");          // printing the new list         System.out.println("LinkedList after insertion using offerFirst() : " + list);     } } 

Output
The initial Linked list is : [Geeks, 4, Geeks, 8] LinkedList after insertion using offerFirst() : [Geek1, Geeks, 4, Geeks, 8] 

Explanation: In the above example, we use the offerFirst() method to add “Geek1” to the first position of a LinkedList.

3. offerLast() Method

This method is used to add an element in the end of the list or we can say it insert the element in the last position in a LinkedList. It is similar to the add() and offer() method which add the element in the last of a LinkedList.

Syntax:

public void offerLast(E e)

  • Parameter: This method takes a single parameter which added in the last of a LinkedList.
  • Return Type: This method does not return anything but modify the LinkedList and add the element in the last position passes as an argument.

Example: Adding an element in the last position using the method offerLast( E e).

Java
// Java code to demonstrate the working // of offerLast(E e) in linked list import java.util.*; public class Geeks {  public static void main(String[] args) 	{ 		// Declaring a LinkedList 		LinkedList list = new LinkedList();  		// adding elements 		list.add("Geeks"); 		list.add(4); 		list.add("Geeks"); 		list.add(8);  		// printing the list 		System.out.println("The initial Linked list is : " + list);  		// offering a new element 		// adds element at end. 		list.offerLast("Geek3");  		// printing the new list 		System.out.println("LinkedList after insertion using offerLast() : " + list); 	} } 

Output
The initial Linked list is : [Geeks, 4, Geeks, 8] LinkedList after insertion using offerLast() : [Geeks, 4, Geeks, 8, Geek3] 

Explanation: In the above example, we use the offerLast() method which adds “Geek3” to the last position of a LinkedList.


Priority Based Sorting Using offerFirst() and offerLast()

Example: Using the offerFirst() and offerLas() methods of LinkedList to perform practical operation on priority addition in queues where elements having a greater number than threshold.

Java
// Java program to demonstrate the application // of offer() in linked list import java.util.*; public class Geeks {  public static void main(String[] args) 	{ 		// Declaring LinkedLists 		LinkedList<Integer> list = new LinkedList<Integer>(); 		LinkedList prioList = new LinkedList();  		// adding elements 		list.add(12); 		list.add(4); 		list.add(8); 		list.add(10); 		list.add(3); 		list.add(15);  		// declaring threshold 		int thres = 10;  		// printing the list 		System.out.println("The initial Linked list is : " + list);  		while (!list.isEmpty()) {  			int t = list.poll();  			// adding >=10 numbers at front rest at back 			if (t >= 10) 				prioList.offerFirst(t); 			else 				prioList.offerLast(t); 		}  		System.out.println("The prioritized Linked list is : " + prioList); 	} } 

Output
The initial Linked list is : [12, 4, 8, 10, 3, 15] The prioritized Linked list is : [15, 10, 12, 4, 8, 3] 

Explanation: In the above example, we use methods offer(), offerFirst() and offerLast() to perform the operation to add the element on the basis of their priority this is the real life example where we need to use these methods.



Next Article
Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
https://media.geeksforgeeks.org/auth/avatar.png
GeeksforGeeks
Improve
Article Tags :
  • Java
  • Java - util package
  • Java-Collections
  • Java-Functions
  • java-LinkedList
  • Linked Lists
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

  • LinkedList addAll() Method in Java
    In Java, the addAll() method of the LinkedList class is used to add all the elements of one collection to another. This method takes a Collection as an argument and adds all its elements to the end of the list. Example: Here, we use the addAll() method to add all the elements of one collection to an
    3 min read
  • LinkedList addFirst() Method in Java
    In Java, the addFirst() method of LinkedList, adds elements at the beginning of the list. All the existing elements moved one position to the right and the new element is placed at the beginning. Syntax of LinkedList addFirst() Method public void addFirst( E e) Parameters: E is the data type of elem
    1 min read
  • LinkedList addLast() Method in Java
    In Java, the addLast() method of the LinkedList class is used to add an element at the end of the list. Syntax of LinkedList addLast() Method void addLast( E e) Parameter: e is the element you want to add at the end of the list.Return type: This method does not return any value.Example: Here, we use
    1 min read
  • LinkedList clear() Method in Java
    In Java, the clear() is used to remove all the elements from a LinkedList. This method only clears all the element from the list and not deletes the list. After calling this method, the list will be empty. Syntax of LinkedList clear() Methodvoid clear() Parameters: This method does not accept any pa
    1 min read
  • LinkedList clone() Method in Java
    In Java, the clone() method of LinkedList, creates a shallow copy which means the structure is duplicated but the objects inside the list are shared between the original and the copied list. So, the cloned list will have the same elements as the original list. Syntax of Java LinkedList clone() Metho
    1 min read
  • LinkedList contains() Method in Java
    In Java, the contains() method of LinkedList is used to check whether an element is present in a LinkedList or not. It takes the element as a parameter and returns True if the element is present in the list. Syntax of Java LinkedList contains() Method boolean contains(Object element);Parameter: The
    2 min read
  • LinkedList descendingIterator() Method in Java
    In Java, the descendingIterator() method of LinkedList is used to return an iterator that allows to traverse the list in reverse order. Example 1: This example demonstrates how to use descendingIterator() method with Strings in a LinkedList. [GFGTABS] Java // Java program to use // descendingIterato
    3 min read
  • LinkedList element() Method in Java
    In Java, the element() method of the LinkedList class is used to retrieve the first element in the list without removing it. The first element of the LinkedList is known as the head. Example 1: Here, we use the element() method to retrieve the first element of the LinkedList of Integers, without rem
    2 min read
  • Java.util.LinkedList.get(), getFirst(), getLast() in Java
    In Java, the LinkedList class provides several methods for accessing the elements in the list. Here are the methods for getting the elements of a LinkedList: get(int index): This method returns the element at the specified position in the LinkedList. The index parameter is zero-based, so the first e
    5 min read
  • LinkedList getLast() Method in Java
    In Java, the getLast() method in the LinkedList class is used to retrieve the last element of the list. Example 1: Here, we use the getLast() method to retrieve the last element of the list. [GFGTABS] Java // Java Program to Demonstrate the // use of getLast() in LinkedList import java.util.LinkedLi
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences