First define a linked list class, and then implement
cut to the chase, post the picture
![insert](https://img-blog.csdnimg.cn/20200808184807821.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3hpYW9sZTA2MDkwMQ==,size_16,color_FFFFFF,t_70)
Linked list
public class Node {
private Object data;
private Node next;
public Node() {
}
public Node(Object data, Node next) {
super();
this.data = data;
this.next = next;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
Linked list
public class LinkedList implements List{
private Node head;
private int size;
public LinkedList() {
head = new Node(null,null);
}
@Override
public void add(int index, Object obj) {
if(index<0 || index >size) return;
Node curr = head;
for(int i=0;i<index;i++) {
curr = curr.getNext();
}
Node node = new Node();
node.setData(obj);
node.setNext(curr.getNext());
curr.setNext(node);
size++;
}
According to this figure and code, you can easily understand the insertion of the linked list.
Like it, like it and collect it!!!