LinkedList offerLast() method with example in java

Published by user on

The offerLast(E element) method of LinkedList appends the specified element at the end of this linked list. It returns true on the successful insertion of the element in this linked list.

Syntax

boolean offerLast(E e)

Parameters

e is the element to insert in this linked-list

Return Value

It returns true on successful insertion of the specified element in this list

Program

import java.util.LinkedList;

public class LinkedListOfferLastExample {

	public static void main(String[] args) {

		LinkedList<Integer> oddNumbers = new LinkedList<>();
		oddNumbers.add(5);
		oddNumbers.add(7);
		oddNumbers.add(9);
		oddNumbers.add(11);

		System.out.println("Linked List elements: " + oddNumbers);
		oddNumbers.offerLast(13);
		System.out.println("Linked List elements after adding element at last position: " + oddNumbers);
	}
}

Output

Linked List elements: [5, 7, 9, 11]
Linked List elements after adding element at last position: [5, 7, 9, 11, 13]
Categories: Java

0 Comments

Leave a Reply

Avatar placeholder

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