Спасибо!
private Node head;
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.
//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.
for (int i = 0; i < index; i++) {
head = head.next;
}
//KEEP THIS LINE TO PRINT RESULT!
System.out.println("Element " + element + " was added at index " + printIndex + ".");
}
}
public class Node {
private int data;
private Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}