publicvoidaddAtIndex(int index,int val){// If index is greater than the length,// the node will not be inserted.if(index > size)return;if(index<0) index =0;++size;
ListNode pred = head;for(int i =0; i < index; i++){
pred = pred.next;}
ListNode add =newListNode(val);
add.next = pred.next;
pred.next = add;}
删除单向链表元素
publicvoiddeleteAtIndex(int index){if(index <0|| index >= size)return;
size--;
ListNode pred = head;for(int i =0; i < index; i++){
pred = pred.next;}
pred = pred.next.next
}
查找单向链表
publicintget(int index){
ListNode curr = head;for(int i =0; i < index+1; i++){
curr = curr.next
}return curr.val;}