Спасибо!
public class Node {
private int data;
private Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public void addByIndex(int index, int element) {
int printIndex = index;
Node newNode = new Node(element);
if (index == 0) {
//Provide implementation where the index at which to add an element is 0.
head = newNode; // head = new Node(element)
//KEEP THIS LINE TO PRINT RESULT!
System.out.println("Element " + element + " was added at index " + index + ".");
} else {
//Provide implementation where the index at which to add an element is greater than 0.
int counter = 0;
while(head != null) {
if (counter == printIndex) {
head = newNode;
break;
}
counter++;
head = head.next;
}
//KEEP THIS LINE TO PRINT RESULT!
System.out.println("Element " + element + " was added at index " + printIndex + ".");
}
}