LinkedList poll() method with example in java
The poll() method of LinkedList retrieves and removes the first element of this linked list.
Syntax
Object poll()
Parameters
It does not take any parameter
Return Value
It returns the removed element from this linked-list
Program
import java.util.LinkedList; public class LinkedListPollExample { public static void main(String[] args) { LinkedListoddNumbers = new LinkedList(); oddNumbers.add(5); oddNumbers.add(7); oddNumbers.add(9); oddNumbers.add(11); System.out.println("Linked List elements: " + oddNumbers); System.out.println("first element removed: " + oddNumbers.poll()); System.out.println("Linked List elements after removing first element: " + oddNumbers); } }
Output
Linked List elements: [5, 7, 9, 11] first element removed: 5 Linked List elements after removing first element: [7, 9, 11]
0 Comments