LinkedListmyList = new LinkedList<>(); myList.add(1); myList.add(2); myList.add(3); int lastElement = myList.getLast(); System.out.println(lastElement); // output: 3
LinkedListPackage Library: java.utilmyList = new LinkedList<>(); myList.add("apple"); myList.add("banana"); myList.add("carrot"); String lastElement = myList.getLast(); System.out.println(lastElement); // output: carrot