public class ListNode<E> {
    private E elem;			// one list element
    private ListNode<E> next;		// next element
    public ListNode(E elem, ListNode<E> next) {
        this.elem = elem;
        this.next = next;
    }
    public E get(int i) {
        if (i == 0) {  return this.elem;  }
        if (this.next == null) {  throw new IndexOutOfBoundsException();  }
        return this.next.get(i-1);
    }
    public void set(int i, E elem) {
        if (i == 0) {  this.elem = elem;  } else {
            if (this.next == null) {  throw new IndexOutOfBoundsException();  }
            this.next.set(i-1, elem);
        }
    }
    public int size() {
        int result = 1;
        if (this.next != null) {  result += this.next.size();  }
        return result;
    }
    public void add(E elem) {
        if (this.next != null) {
            this.next.add(elem);
        } else {
            this.next = new ListNode<E>(elem, null);
        }
    }
    public void add(int i, E elem) {
        if (i == 0) {
            this.next = new ListNode<E>(this.elem, this.next);  // duplicate
            this.elem = elem;
        } else {
            if (this.next == null) {
                if (i == 1) {  this.add(elem);  }	// end of list
                else {  throw new IndexOutOfBoundsException();  }
            }  else {  this.next.add(i-1, elem);  }
        }
    }
    public void remove(int i) {
        if (this.next == null) {  throw new IndexOutOfBoundsException();  }
        if (i == 1) {
            this.next = this.next.next;
        } else {
            this.next.remove(i-1);
        }
    }
    public String toString() {
        return this.elem+(this.next == null ? "" : ", "+this.next.toString());
    }
    public ListNode<E> getNext() {
        return this.next;
    }
    public void setNext(ListNode<E> next) {
        this.next = next;
    }
    public E getElem() {
        return this.elem;
    }
    public void setElem(E elem) {
        this.elem = elem;
    }
}
