Single-linked list---ring-related issues (whether there is a ring, whether it intersects)

Source: Internet
Author: User

One, two single-linked list without ring--->

    1. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.