LinkedListnames = new LinkedList<>(); names.add("John"); names.add("Jane"); names.add("Alex"); System.out.println(names.size()); // Output: 3
LinkedListIn this example, we create a LinkedList of Integers and add three elements to it. We then remove the first and last elements from the list and print the number of elements remaining in the list using the size() method, which is 1. The LinkedList class is part of the java.util package library.numbers = new LinkedList<>(); numbers.add(10); numbers.add(20); numbers.add(30); numbers.removeFirst(); numbers.removeLast(); System.out.println(numbers.size()); // Output: 1