什么网站是html5做的,wordpress付费阅读全文,网站论坛推广文案怎么做,自建域名** ** 结论 让一个指针从链表起始位置开始遍历链表#xff0c;同时让一个指针从判环时相遇点的位置开始绕环运行#xff0c;两个指针都是每次均走一步#xff0c;最终肯定会在入口点的位置相遇。 LinkedList的模拟实现 单个节点的实现 尾插 运行结果如下#xff1a; 也… ** ** 结论 让一个指针从链表起始位置开始遍历链表同时让一个指针从判环时相遇点的位置开始绕环运行两个指针都是每次均走一步最终肯定会在入口点的位置相遇。 LinkedList的模拟实现 单个节点的实现 尾插 运行结果如下 也可以暴力使用 全部代码 MyLinkedList
public class MyLinkedList {static class ListNode{public int val;public ListNode prev;public ListNode next;public ListNode(int val) {this.val val;}}public ListNode head;public ListNode last;//链表的长度public int size(){int len0;ListNode curhead;while(cur!null){curcur.next;len;}return len;}//打印链表public void dispaly(){ListNode curhead;while(cur!null){System.out.print(cur.val );curcur.next;}System.out.println();}//查找关键字key是否在链表中public boolean contains(int key){ListNode curhead;while(cur!null){if(cur.valkey){return true;}curcur.next;}return false;}//头插
public void addFirst(int data){ListNode nodenew ListNode(data);//空链表if(headnull){headnode;lastnode;return;}//一般情况node.nexthead;head.prevnode;headnode;}//尾插public void addLast(int data){ListNode nodenew ListNode(data);//空链表if(headnull){headnode;lastnode;return;}//一般情况last.nextnode;node.prevlast;lastnode;}
//无论是头插还是尾插只要是空链表那么新增节点的next和prev都引用自身//任意位置的插入在位置后面插入public void AddIndex(int index,int data){ListNode nodenew ListNode(data);if(index0||indexsize()){throw new IndexOutOfBounds(任意位置插入坐标非法非法值为index);}if(index0){addFirst(data);return;}if(indexsize()){addLast(data);return;}//一般情况ListNode curhead;while(index!0){curcur.next;index--;}node.nextcur;cur.prev.nextnode;node.prevcur.prev;cur.prevnode;}//删除第一次出现关键字为key的节点public void remove(int key){//链表为空if(headnull){return;}//只有一个节点并且该节点值为keyif(head.nextnullhead.valkey){head.prevnull;head.nextnull;return;}//头节点if(head.valkey){head.next.prevnull;headhead.next;return;}//尾节点if(last.valkey){last.prev.nextnull;lastlast.prev;return;}//一般情况ListNode curhead;while(cur!null){if(cur.valkey){cur.prev.nextcur.next;cur.next.prevcur.prev;return;}curcur.next;}}//删除所有出现关键字为key的节点public void removeAll(int key){//链表为空if(headnull){return;}//只有一个节点并且该节点值为keyif(head.nextnullhead.valkey){head.prevnull;head.nextnull;return;}//头节点if(head.valkey){head.next.prevnull;headhead.next;}//尾节点if(last.valkey){last.prev.nextnull;lastlast.prev;}//一般情况ListNode curhead;while(cur!null){if(cur.valkey){cur.prev.nextcur.next;cur.next.prevcur.prev;}curcur.next;}}//clearpublic void clear(){
/* ListNode curhead;while(cur!null){ListNode curNextcur.next;//存储下一个节点方便下次循环使用cur.prevnull;cur.nextnull;curcurNext;//更新当前节点}*/headnull;lastnull;//使头节点和尾节点的地址置空}}IndexOutOfBounds
public class IndexOutOfBounds extends RuntimeException{public IndexOutOfBounds() {}public IndexOutOfBounds(String message) {super(message);}
}Test1
public class Test1 {public static void main(String[] args) {MyLinkedList myLinkedListnew MyLinkedList();myLinkedList.addFirst(34);myLinkedList.addFirst(3);myLinkedList.addFirst(4);myLinkedList.addFirst(340);myLinkedList.addFirst(344);myLinkedList.dispaly();System.out.println(myLinkedList.size());System.out.println(myLinkedList.contains(4));System.out.println(我是分割线);myLinkedList.addLast(-12);myLinkedList.addLast(-121);myLinkedList.addLast(-1222);myLinkedList.dispaly();myLinkedList.AddIndex(3,999);myLinkedList.dispaly();// myLinkedList.AddIndex(55,999);System.out.println(我是分割线);myLinkedList.remove(999);myLinkedList.dispaly();myLinkedList.remove(344);myLinkedList.dispaly();myLinkedList.addFirst(-12);myLinkedList.addLast(-12);myLinkedList.dispaly();myLinkedList.removeAll(-12);myLinkedList.dispaly();System.out.println(我是分割线);myLinkedList.clear();myLinkedList.dispaly();System.out.println(证明有空行);}
}什么是LinkedList LinkedList的底层是双向链表结构(链表后面介绍)由于链表没有将元素存储在连续的空间中元素存储在单独的节点中然后通过引用将节点连接起来了因此在在任意位置插入或者删除元素时不需要搬移元素效率比较高。 LinkedList的使用 LinkedList的其他常用方法 LinkedList的遍历 ArrayList和LinkedList的区别