import java.util.NoSuchElementException; public class LinkedList> { private static class Node { private T value; private Node next; private Node(T value, Node next) { this.value = value; this.next = next; } } private Node head = null; private int size; public void addFirst(E element) { if (element == null) { throw new NullPointerException(); } head = new Node(element, head); size++; } public int size() { return size(head); } private int size(Node current) { int count = 0; if (current != null) { count = 1 + size(current.next); } return count; } public String toString() { String str = "["; Node current = head; while (current != null) { if (current != head) { str += ","; } str += current.value; current = current.next; } str += "]"; return str; } public static void main(String[] args) { LinkedList xs = new LinkedList(); for (int i=4; i>=0; i--) { xs.addFirst(i); } System.out.println(xs); System.out.println(xs.size()); } }