首先写一个链表.设置一个闭环

    1. package com.ytt.demo.dataStructure;
    2. import com.sun.org.apache.xalan.internal.xsltc.dom.StepIterator;
    3. import com.sun.tools.javap.TypeAnnotationWriter;
    4. public class SingleLinkedList<T> {
    5. private int size;
    6. private Node<T> head;
    7. /**
    8. * 添加
    9. * @param v
    10. */
    11. public void add(T v){
    12. if(size>0){
    13. Node<T> node=new Node<>(v,null);
    14. Node lastNode = getLastNode();
    15. if(lastNode!=null){
    16. lastNode.setNext(node);
    17. size++;
    18. }
    19. }else {
    20. if(head==null){
    21. head=new Node<>(v,null);
    22. }else {
    23. head.setData(v);
    24. }
    25. size++;
    26. }
    27. }
    28. public SingleLinkedList(){
    29. size=0;
    30. head=new Node<>(null,null);
    31. }
    32. /**
    33. * 具体位置添加
    34. * @param position
    35. * @param v
    36. */
    37. public void insert(int position, T v){
    38. if(position>=0 && position<size){
    39. Node insertNode=new Node(v,null);
    40. Node node=head;
    41. Node pre=null;
    42. if(position==0){
    43. insertNode.setNext(head);
    44. head=insertNode;
    45. size++;
    46. }else {
    47. for(int x=0;x<position;x++){
    48. pre=node;
    49. node=node.getNext();
    50. }
    51. pre.setNext(insertNode);
    52. insertNode.setNext(node);
    53. size++;
    54. }
    55. }
    56. }
    57. /**
    58. * 删除指定位置
    59. * @param position
    60. * @return
    61. */
    62. public T remove(int position)
    63. {
    64. if(position>=0 && position<size){
    65. Node<T> node=head;
    66. Node<T> pre=null;
    67. Node<T> next=null;
    68. T value=null;
    69. if(position==0){
    70. value= node.getData();
    71. node= node.getNext();
    72. head.setNext(null);
    73. head.setData(null);
    74. head=node;
    75. size--;
    76. }else {
    77. for(int x=0;x<position;x++){
    78. //找到删除的节点
    79. pre=node;//当前要删除的上个节点
    80. node=node.getNext();//当前节点
    81. next=node.getNext();//当前需要删除的下个节点
    82. }
    83. value= (T) node.getData();
    84. node.setNext(null);
    85. node.setData(null);
    86. node=null;
    87. pre.setNext(next);
    88. size--;
    89. }
    90. return value;
    91. }
    92. return null;
    93. }
    94. /**
    95. * 获取指定位置节点
    96. * @param position
    97. * @return
    98. */
    99. public Node getNode(int position){
    100. if(position>=0 && position<size){
    101. Node node=head;
    102. for(int x=0;x<position;x++){
    103. node=node.getNext();
    104. }
    105. return node;
    106. }else {
    107. return null;
    108. }
    109. }
    110. public Node getHead(){
    111. return head;
    112. }
    113. /**
    114. * 获取最后一个节点
    115. * @return
    116. */
    117. public Node getLastNode(){
    118. Node node=head;
    119. while (node !=null && node.getNext()!=null){
    120. node= node.getNext();
    121. }
    122. return node;
    123. }
    124. /**
    125. * 清空
    126. *
    127. */
    128. public void clear(){
    129. for (int x=0;x<size;x++){
    130. System.out.println("删除前:"+head.getData());
    131. Node next=head.getNext();
    132. head.setNext(null);
    133. head.setData(null);
    134. System.out.println("删除后:"+head.getData());
    135. head= next;
    136. }
    137. size=0;
    138. }
    139. /**
    140. * 查看第一个元素的数据
    141. * @return
    142. */
    143. public T peek(){
    144. if(size>0 && head!=null){
    145. return head.getData();
    146. }
    147. return null;
    148. }
    149. /**
    150. * 反转打印
    151. */
    152. public Node reversal(){
    153. if (head == null){
    154. return head;
    155. }
    156. Node node=head;
    157. Node pre=null;
    158. Node cur=node;
    159. Node next=node.getNext();
    160. //1->2->3->4->5
    161. while (next!=null){
    162. next=cur.getNext();//记录当前节点的下一个节点
    163. cur.setNext(pre);//将当前节点的下一个节点指向当前节点的上一个节点//交换位置
    164. pre=cur;//前一个节点后移到当前节点
    165. cur=next;//当前节点设置成后一个节点
    166. }
    167. return pre;
    168. }
    169. public Node reversal2(Node<T> node)
    170. {
    171. if (node == null || node.getNext()==null){
    172. System.out.println(node.getData());
    173. return node;
    174. }
    175. Node<T> node1 = reversal2(node.getNext());
    176. node.getNext().setNext(node);
    177. node.setNext(null);
    178. return node1;
    179. }
    180. /**
    181. * 正序打印
    182. */
    183. public void print(Node n){
    184. Node<T> node=n;
    185. while (node!=null){
    186. String data = (String) node.getData();
    187. System.out.println("data:"+data);
    188. node=node.getNext();
    189. }
    190. }
    191. public boolean isEmpty(){
    192. if(size==0 ){
    193. return true;
    194. }
    195. return false;
    196. }
    197. public int getSize(){
    198. return size;
    199. }
    200. public void setClosedIndex(int index){
    201. if(size>0 && index>0 && index<size){
    202. Node lastNode = getLastNode();
    203. Node node = getNode(index);
    204. lastNode.setNext(node);
    205. }
    206. }
    207. public Node<T> getCollideNode(){
    208. Node slow=head;
    209. Node fast=head;
    210. while (slow!=null && fast!=null ){
    211. slow=slow.getNext();
    212. if(fast.getNext()!=null){
    213. fast=fast.getNext().getNext();
    214. }else {
    215. return null;
    216. }
    217. if(slow==null || fast ==null){
    218. return null;
    219. }
    220. if(slow==fast){
    221. return fast;
    222. }
    223. }
    224. return null;
    225. }
    226. public int getStartIndex(){
    227. Node<T> collideNode = getCollideNode();
    228. if(collideNode!=null){
    229. System.out.println("碰撞 data="+collideNode.getData());
    230. Node<T> mFast=collideNode;
    231. Node<T> mSlow=head;
    232. int index=0;
    233. while (mFast!=mSlow){
    234. mSlow=mSlow.getNext();
    235. mFast=mFast.getNext();
    236. index++;
    237. }
    238. return index;
    239. }
    240. return -1;
    241. }
    242. }
     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);
    
    
        }