首先写一个链表.设置一个闭环
package com.ytt.demo.dataStructure;import com.sun.org.apache.xalan.internal.xsltc.dom.StepIterator;import com.sun.tools.javap.TypeAnnotationWriter;public class SingleLinkedList<T> {private int size;private Node<T> head;/*** 添加* @param v*/public void add(T v){if(size>0){Node<T> node=new Node<>(v,null);Node lastNode = getLastNode();if(lastNode!=null){lastNode.setNext(node);size++;}}else {if(head==null){head=new Node<>(v,null);}else {head.setData(v);}size++;}}public SingleLinkedList(){size=0;head=new Node<>(null,null);}/*** 具体位置添加* @param position* @param v*/public void insert(int position, T v){if(position>=0 && position<size){Node insertNode=new Node(v,null);Node node=head;Node pre=null;if(position==0){insertNode.setNext(head);head=insertNode;size++;}else {for(int x=0;x<position;x++){pre=node;node=node.getNext();}pre.setNext(insertNode);insertNode.setNext(node);size++;}}}/*** 删除指定位置* @param position* @return*/public T remove(int position){if(position>=0 && position<size){Node<T> node=head;Node<T> pre=null;Node<T> next=null;T value=null;if(position==0){value= node.getData();node= node.getNext();head.setNext(null);head.setData(null);head=node;size--;}else {for(int x=0;x<position;x++){//找到删除的节点pre=node;//当前要删除的上个节点node=node.getNext();//当前节点next=node.getNext();//当前需要删除的下个节点}value= (T) node.getData();node.setNext(null);node.setData(null);node=null;pre.setNext(next);size--;}return value;}return null;}/*** 获取指定位置节点* @param position* @return*/public Node getNode(int position){if(position>=0 && position<size){Node node=head;for(int x=0;x<position;x++){node=node.getNext();}return node;}else {return null;}}public Node getHead(){return head;}/*** 获取最后一个节点* @return*/public Node getLastNode(){Node node=head;while (node !=null && node.getNext()!=null){node= node.getNext();}return node;}/*** 清空**/public void clear(){for (int x=0;x<size;x++){System.out.println("删除前:"+head.getData());Node next=head.getNext();head.setNext(null);head.setData(null);System.out.println("删除后:"+head.getData());head= next;}size=0;}/*** 查看第一个元素的数据* @return*/public T peek(){if(size>0 && head!=null){return head.getData();}return null;}/*** 反转打印*/public Node reversal(){if (head == null){return head;}Node node=head;Node pre=null;Node cur=node;Node next=node.getNext();//1->2->3->4->5while (next!=null){next=cur.getNext();//记录当前节点的下一个节点cur.setNext(pre);//将当前节点的下一个节点指向当前节点的上一个节点//交换位置pre=cur;//前一个节点后移到当前节点cur=next;//当前节点设置成后一个节点}return pre;}public Node reversal2(Node<T> node){if (node == null || node.getNext()==null){System.out.println(node.getData());return node;}Node<T> node1 = reversal2(node.getNext());node.getNext().setNext(node);node.setNext(null);return node1;}/*** 正序打印*/public void print(Node n){Node<T> node=n;while (node!=null){String data = (String) node.getData();System.out.println("data:"+data);node=node.getNext();}}public boolean isEmpty(){if(size==0 ){return true;}return false;}public int getSize(){return size;}public void setClosedIndex(int index){if(size>0 && index>0 && index<size){Node lastNode = getLastNode();Node node = getNode(index);lastNode.setNext(node);}}public Node<T> getCollideNode(){Node slow=head;Node fast=head;while (slow!=null && fast!=null ){slow=slow.getNext();if(fast.getNext()!=null){fast=fast.getNext().getNext();}else {return null;}if(slow==null || fast ==null){return null;}if(slow==fast){return fast;}}return null;}public int getStartIndex(){Node<T> collideNode = getCollideNode();if(collideNode!=null){System.out.println("碰撞 data="+collideNode.getData());Node<T> mFast=collideNode;Node<T> mSlow=head;int index=0;while (mFast!=mSlow){mSlow=mSlow.getNext();mFast=mFast.getNext();index++;}return index;}return -1;}}
public static void main(String[] args) {
SingleLinkedList<String> linkedList=new SingleLinkedList<>();
linkedList.add("1");
linkedList.add("2");
linkedList.add("3");
linkedList.add("4");
linkedList.add("5");
linkedList.add("6");
//设置闭环开始位置为2
linkedList.setClosedIndex(2);
//找到闭环开位置
int startIndex = linkedList.getStartIndex();
System.out.println("startIndex="+startIndex);
}
