/ *
Is determined whether there is a single chain ring
1 ) Violence: double loops through (^ n- 2 ) 2 ) double Pointer: Pointer fast fast = NULL, slow pointer = SLOW NULL int Judge (Link head) { IF (head == NULL) return to false ; Link FAST = head , SLOW = head; the while (SLOW FAST = = NULL &&!! NULL) { SLOW = slow-> Next; FAST = FaST - PAK> next-> Next; IF (== SLOW FAST) { return to true ; } } return false; }
Calculating the length of the belt loop of single chain
int count(link head) { if(NULL==head) return false; link fast = head, slow = head; while(slow!=NULL && fast!=NULL) { slow = slow->next; fast = fast->next->next; if(slow==fast) { flag = true; break; } } if(flag) { int ans = 1; slow = slow->next; while(slow!=fast) { ans++; slow = slow->next; } return ans ; } return false; }
Single linked list lookup entry ring
设环长为n,非环形部分长度为m link find(link head) { int flag = false; if(NULL==head) return false; link fast = head, slow = head; while(slow!=NULL && fast!=NULL) { slow = slow->next; fast = fast->next->next; if(slow==fast) { flag=true; break; } } if(flag) { slow = head; while(slow!=fast) { slow = slow->next; fast = fast->next; } return slow; } return NULL; }