LinkedListmyList = new LinkedList (); myList.add("apple"); myList.add("banana"); myList.add("orange"); System.out.println("Before removing the first element: " + myList); String firstElement = myList.removeFirst(); System.out.println("First element removed: " + firstElement); System.out.println("After removing the first element: " + myList);
Before removing the first element: [apple, banana, orange] First element removed: apple After removing the first element: [banana, orange]
LinkedListmyList = new LinkedList (); myList.add(10); myList.add(20); myList.add(30); System.out.println("Before removing the first element: " + myList); Integer firstElement = myList.removeFirst(); System.out.println("First element removed: " + firstElement); System.out.println("After removing the first element: " + myList);
Before removing the first element: [10, 20, 30] First element removed: 10 After removing the first element: [20, 30]In both examples, the `removeFirst()` method is used to remove the first element of the linked list and return its value. The package library used is `java.util.LinkedList`.