Java Program to Retrieve and Remove the Head Item From the LinkedList

You are currently viewing Java Program to Retrieve and Remove the Head Item From the LinkedList

Java program to retrieve and remove the head item from the LinkedList

// Java program to retrieve and remove the head 
// item from the LinkedList

import java.util.LinkedList;

public class Main {
  public static void main(String[] args) {
    LinkedList list = new LinkedList();

    list.add(1);
    list.add("TWO");
    list.add(3);
    list.add("FOUR");
    list.add(true);

    System.out.println("List Items: \n" + list);
    System.out.println("Head Item: " + list.poll());
    System.out.println("List Items: \n" + list);
    System.out.println("Head Item: " + list.poll());
    System.out.println("List Items: \n" + list);
  }
}

Output

List Items: 
[1, TWO, 3, FOUR, true]
Head Item: 1
List Items: 
[TWO, 3, FOUR, true]
Head Item: TWO
List Items: 
[3, FOUR, true]

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply