The problem of single-chain watch ring

Source: Internet
Author: User

1. Determine if a single linked list has a ring

With two slow, the fast pointer scans the linked list from the beginning. the pointer slow each walk 1 steps, the pointer fast each walk 2 steps. If there is a ring, the pointer slow, fast will meet , and if there is no ring, the pointer fast encounters a null exit.

is the so-called chase encounter Problem:

    

The code examples are as follows:

Determine if the chain list has a ring, if no ring returns nulllistnode* Hascircle (Linklist head) {if (head==null) return NULL; ListNode *slow=head; ListNode *fast=head;while (fast!=null) {slow=slow->next;//Go one step fast=fast->next;if (Fast!=NULL) fast=fast-> Next;elsereturn null;//If slow==fast, then there is a ring if (slow==fast) return slow;} Exit Loop return NULL if the while condition is false;}


2. Find the ring connection point position of the ring single linked list

The distance of the first collision Point Pos to the connection point join = the distance from the head pointer to   the join point, so, starting with the first collision point Pos and head pointer Head, the point of encounter is the connection point.

After meeting on the ring, record the first meeting point for POS, connection point is join, assuming that the length of the head node to the connection point is LenA, the connection point to the first meeting point length is x, the ring length is R.

The first time we met, slow walked the length S = LenA + x;

The first time you meet, the length of fast walk 2S = LenA + nR + x;

So you know,LenA + x = nR; LenA = n*r-x;

The code examples are as follows:

Determines whether a linked list has a ring, and if so, returns the entry point of the ring, or nulllistnode* circleentrance (linklist head) {if (head==null) return NULL If there is no ring; ListNode *slow=head; ListNode *fast=head;while (fast!=null) {slow=slow->next;//Go one step fast=fast->next;if (Fast!=NULL) fast=fast-> Next;elsereturn null;//If slow==fast, then there is a ring if (slow==fast) break;} While condition is false, exit loop if (fast==null)//non-ring exit return null;else{//seek entry point position slow=head;while (slow!=fast) {slow=slow->next; Fast=fast->next;} return slow;}}


3. The ring length of a single linked list

The length of the ring can be easily obtained by the beginning of the known ring.

The sample code is as follows:

If a linked list has a ring, returns the length of the ring, or no ring, returns 0int Circlelength (Linklist head) {if (head==null) return 0; ListNode *slow=head; ListNode *fast=head;while (fast!=null) {slow=slow->next;//Go one step fast=fast->next;if (Fast!=NULL) fast=fast-> Next;elsereturn 0;//If slow==fast, then there is a ring if (slow==fast) break;} If the while condition is false, exit the loop if (fast==null)//No Ring exit return 0;else{slow=head;while (slow!=fast) {slow=slow->next;fast=fast- >next;} while () slow points to int circlelen=1; ListNode *p=slow;//The length of the ring while (P->next!=slow) {++circlelen;p=p->next;} return Circlelen;}}

4. Find the chain list length with ring single linked list

The length of the ring is calculated in the above 3, the position of the connection point is obtained in 2, and the length of the head node to the connection point can be obtained. The sum of the two is the length of the linked list.

5. Tail interpolation method to establish a linked list

typedef struct LISTNODE{INT data;struct listnode* next;} listnode,*linklist;//tail interpolation creates a linked list with a ring linklist createlinklist () {linklist head=null; ListNode *pre=null,*inter=null;//tail interpolation establishes a linked list for (int i=1;i<=6;i++) {//Create node ListNode *temp= (listnode*) malloc (sizeof ( ListNode)); Temp->data=i;temp->next=null;if (head==null) {head=temp;pre=head;} else{pre->next=temp;pre=temp;//tail interpolation method}if (i==6) inter=temp;//i==3 value at the intersection of}pre->next=inter;//to establish a linked list of the ring return head;}

6. Test code
int main () {linklist head=createlinklist (); int circlelen=circlelength (head); if (!circlelen) cout<< "No Circle" <<endl;else{cout<< "Have circle" <<endl;cout<< "circle entrance is" <<circlelen<< Endl;} return 0;}

The problem of single-chain watch ring

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.