
public class LinkedList2<E>
{
    private ListNode<E> head;

    public LinkedList2( )
    {
        head = null;
    }

    public int length( )
    {
        int count = 0;
        ListNode<E> position = head;
        while (position != null)
        {
            count++;
            position = position.getLink( );
        }
        return count;
    }

    public void addANodeToStart(E addData)
    {
        head = new ListNode<E>(addData, head);
    }

    public void deleteHeadNode( )
    {
        if (head != null)
        {
            head = head.getLink( );
        }
        else
        {
            System.out.println("Deleting from an empty list.");
            System.exit(0);
        }
    }

    public boolean onList(E target)
    {
        return (Find(target) != null);
    }

    private ListNode<E> Find(E target)
    {
        ListNode<E> position;
        position = head;
        E dataAtPosition;
        while (position != null)
        {
            dataAtPosition = position.getData( );
            if (dataAtPosition.equals(target))
                return position;
            position = position.getLink( );
        }
        //target was not found.
        return null;
    }

    public void showList( )
    {
        ListNode<E> position;
        position = head;
        while (position != null)
        {
            System.out.println(position.getData( ));
            position = position.getLink( );
        }
    }
}
