public class LinkedList { private static class Node { private E value; private Node next; private Node( E value, Node next ) { this.value = value; this.next = next; } } private Node head = null; public void addFirst( E o ) { if ( o == null ) { throw new IllegalArgumentException( "null" ); } head = new Node( o, head ); } public int size() { return size( head ); } private int size( Node p ) { if ( p == null ) { return 0; } return 1 + size( p.next ); } public static void main( String[] args ) { LinkedList l = new LinkedList(); for ( int i=0; i<5; i++ ) { l.addFirst( i ); } System.out.println( "length = " + l.size() ); } }