The insert method adds an element to a specific position, shifting the rest to the right. Here is my code:
public void insert(Object data, int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } Node node = new Node(null,data); Node cur = head ; // ссылка на начало if (index == 0) { if (size == 0) { head = node; size = 1; } else { node.next = head; head = node; size++; } } else{ for(int i = 0 ; i<index && cur.next!=null ; i++) // находим элемент с этим индексом cur = cur.next ; node.next = cur ; // говорим , что после вставляемого элемента, будет cur size++; } } But I assume that the problem is that I did not write after which element the inserted (new) element will be located. How can I do that?