trying to find the best way to do the "TODO's" for my add method. need to handle the special case where we are adding to the beginning of the list (index = 0) and need to handle the special case where the list is empty and index = 0
here is the method:
public void add(int index, T item) throws IllegalArgumentException {
if (index < 0 || index > size) {
throw new IllegalArgumentException();
} else {
// TODO need to handle the special case where we are adding
// to the beginning of the list (index = 0)
// TODO need to handle the special case where the list is empty
// and index = 0
int counter = 0;
Node<T> current = head;
while (counter < index-1) {
counter++;
current = current.next;
}
Node<T> newNode = new Node(item);
newNode.next = current.next;
current.next = newNode;
size++;
}
}