package linkedlist;

public class LinkedList2<T> {

    Node<T> head;

    public LinkedList2() {}

    public void addFirst(T value) {

        if (head == null) {

            head = new Node<T>(value, null);

        } else {

            Node<T> newHead = new Node<T>(value, head);

            head = newHead;

        }

    }

     

    public void addLast(T value) {

        if (head == null) {

            head = new Node<T>(value, null);

        } else {

             Node<T> lastNode = head;

             while (lastNode.getNext() != null) {

                lastNode = lastNode.getNext(); 

            }

            Node<T> node = new Node<T>(value, null);

            lastNode.setNext(node);

        }

    }

     

    public void print() {

         Node<T> lastNode = head;

         while (lastNode != null) {

             System.out.println(lastNode.getValue());

             lastNode = lastNode.getNext();

         }

    }

}