

class LinkedList3
{
    private Node head ;
    private Node tail ;
    /* head == tail == null -> empty list */
 
    void reverseList()
    {
        Node f = null ; // finished part of list
        Node r = head ; // rest of list to do
        // swap head and tail
        Node t = head ;
        head = tail ;
        tail = t ;
        // Assert: Loop Invariant: list is split
        //    between f and r, f being finished, r
        //    needs to be processed.
        while ( r!=null )
        {
           t = r ; // to process t
           r = r.next ; // loop will terminate :-)
           t.next = f ; // put on head
           f = t ; // note: t has been used
        }
    }

    void insertAtHead( String newString )
    {
        Node n = new Node() ;
        n.content = newString ;
        n.next = head ;
        head = n ;
        if ( tail==null ) tail = head ;
    }

    void insertAtTail( String newString )
    {
        if ( head==null )
        {
           insertAtHead( newString ) ;
        }
        else
        {
           Node n = new Node() ;
           n.content = newString ;
           tail.next = n ;
           tail = n ;
        }
    }

    void printList()
    {
        Node n = head ;
        while ( n!=null )
        {
            System.out.println(n.content) ;
            n = n.next ;
        }
    }

}

