一.简介物理上不一定连续逻辑上连续分类共8种单向双向带头不带头循环非循环二.模拟实现MyLinkedList类package LinkedList; //无头单向不循环 public class MyLinkedList { class ListNode{ public int val; public ListNode next; public ListNode(int val) { this.val val; } } public ListNode head;//链表头结点 //遍历打印 public void display(){ ListNode curhead; while(cur!null){ System.out.print(cur.val ); curcur.next; } System.out.println(); } public int size(){ int cnt0; ListNode curhead; while(cur!null){ cnt; curcur.next; } return cnt; } //头插 public void addFirst(int val){ ListNode nodenew ListNode(val); //插入节点时先绑定后面的 node.nexthead; headnode; } //尾插 public void addLast(int val){ ListNode nodenew ListNode(val); //head为空 if(headnull){ headnode; return; } //head不为空 ListNode curhead; while(cur.next!null){ curcur.next; } cur.nextnode; } //插入在指定位置的后面 public void addIndex(int index,int val){ ListNode nodenew ListNode(val); //判断index位置是否合法 try{ checkIndex(index); }catch (IndexNotLegalException e){ e.printStackTrace(); } //index0||indexsize if(index0) { addFirst(val); }else if(indexsize()){ addLast(val); }else{ //找到index前一个位置 ListNode prefindIndexSubOne(index); //插入连接 node.next pre.next; pre.nextnode; } } private void checkIndex(int index)throws IndexNotLegalException{ if(index0||indexsize()){ throw new IndexNotLegalException(index不合法); } } private ListNode findIndexSubOne(int index){ ListNode curhead; int cnt0; while(cnt!index){ curcur.next; cnt; } return cur; } public boolean containsValue(int val){ ListNode curhead; while(cur!null){ if(cur.valval){ return true; } curcur.next; } return false; } //删除第一次出现的节点 public void remove(int val){ if(headnull){ return; } if(head.valval){ headhead.next; return; } ListNode curhead; while(cur.next!null){ if(cur.next.valval){ cur.nextcur.next.next; return; } curcur.next; } } //删除所有的关键字 public void removeAllKey(int val){ if(headnull){ return; } ListNode prehead; ListNode curhead.next; //判断删除 while(cur!null){ if(cur.valval){ pre.nextcur.next; curcur.next; }else{ precur; curcur.next; } } //判断头结点 if(head.valval){ headhead.next; } } public void clear(){ //1 //headnull; //2 ListNode curhead; while(cur!null){ ListNode curNcur.next; cur.nextnull; curcurN; } headnull; } }IndexNotLegalException异常package LinkedList; public class IndexNotLegalException extends RuntimeException { public IndexNotLegalException(){ } public IndexNotLegalException(String msg){ System.out.println(msg); } }三.题目