One, two single-linked list without ring--->
Is there a "ring" and a ring length
Method: With the help of the fast and slow pointer, whether there is a meeting situation (existence, that is, the existence of the ring; otherwise, does not exist)
Ring Length: When we meet, we begin to calculate the distance traveled by the slow pointer, that is, the ring length
int iscycle (ListNode *_head)//whether there is ring and ring length (two linked list without ring) {ListNode *fast=_head; ListNode *slow=_head;while (fast&&fast->_next&&fast->_next->_next) {fast = fast->_next- >_next;slow = slow->_next;if (fast==slow)//existence ring {int Count=0;while (slow->_next==slow) {++count;slow=slow- >_next;} return count;}} return 0;}
2. Intersection problem (two linked list without ring)
Method 1: The end node addresses of the two single linked lists are the same (same as Intersect, not intersect)
Intersection node: Select the short list in the two linked lists, first traverse the longer linked list, until the remaining length of the two linked lists at the same time, and then start timing, until the two meet, the timing of the data is the node when the meeting
int Iscross (ListNode *l1,listnode *l2)//whether Intersect and (two linked list without ring) {ListNode *head=null; ListNode *tail=null;if (l1==null| | L2==null) {printf ("The two-linked list does not intersect \ n"); return 0;} Else{int Len1=0,len2=0;while (L1) {++len1;head=head->_next;} while (L2) {++len2;tail=head->_next;} if (&head==&tail) {int gap=0;int count=0;printf ("two linked lists intersect \ n"); if (len1<len2) {int tmp=len1;len1=len2;len2=tmp ;} Gap=len1-len2;while (L1) {++len1;head=head->_next;if (Len1=gap) {while (L1) {Head=head->_next;while (L2) {+ +) Count;tail=head->_next;if (head==tail) {return count;}}}}} else{printf ("Two-linked list does not intersect \ n"); return 0;}}
This article is from the "Flower Open Shore" blog, please be sure to keep this source http://zxtong.blog.51cto.com/10697148/1757709
Single-linked list---ring-related issues (whether there is a ring, whether it intersects)