To determine the ring in a linked list

Source: Internet
Author: User

1. The first type of implementation

BOOL List_is_loop (Slist *head)

{

Slist *slow=head;

Slist *fast=head;

while (Null!=fast && null!=fast->next)

{

slow=slow->next;

fast=fast->next->next;

if (slow==fast) break;

}

  

No ring: Fast->next is always null when the number of lists is cardinality, and fast is always null when the number of linked lists is even;

Return! (Fast==null | | fast->next==null);

}

2. The second type of implementation

BOOL List_is_loop (Slist *head)

{

Slist *slow=head;

Slist *fast=head;

if (Null==slow | | Null==slow->next)

return false;

  

Do

{

slow=slow->next;

fast=fast->next->next;

}while (null!=fast && null!=fast->next && slow!=fast)

if (slow==fast)

{

return true;

}

Else

{

return false;

}

}

3. if the link list exists, locate the ring entry point

Suppose slow walked the S-step, then fast took 2s steps (the step number of fast is also equal to S plus the N-turns on the ring), the ring length is R, then:
2s=s+nr
S=nr
Set the entire chain table length L, the inlet ring and the meeting point distance is X, the starting point to the ring entry point distance is a, then
A+x=s=nr
a+x= (n-1) r+r= (n-1) r+l-a
A= (n-1) r+ (l-a-x)
(l-a-x) is the distance from the point of encounter to the ring entry point. So, from the chain head to the ring entry point equals (n-1) loop inner ring + meet point to ring entry point. So you can set a pointer from the link table head and meeting point, each step, two pointers must meet, and meet the 1th is the ring entry point.

1---2---3---4--5--6--7
| |

-----------

As shown: In Node 5 encounter, 1 to 4 distance of a;4 to 5 is x; ring length is R;

slist* List_is_loop_enter (slist *head)

{

Slist *slow=head;

Slist *fast=head;

while (Null!=fast && null!=fast->next)

{

slow=slow->next;

fast=fast->next->next;

if (slow==fast) break;

}

  

No ring: Fast->next is always null when the number of lists is cardinality, and fast is always null when the number of linked lists is even;

if (Fast==null | | fast->next==null)

return NULL;

  

Slow=head;

while (Slow!=fast)

{

slow=slow->next;

fast=fast->next;

}

return slow;

}

4. Calculate the ring length

A loop can be performed from the ring inlet.

int List_is_loop_length (slist *head)

{

int length=0;

Slist *slow=head;

Slist *fast=head;

while (Null!=fast && null!=fast->next)

{

slow=slow->next;

fast=fast->next->next;

if (slow==fast) break;

}

  

No ring: Fast->next is always null when the number of lists is cardinality, and fast is always null when the number of linked lists is even;

if (Fast==null | | fast->next==null)

return 0;

  

Slow=head;

while (Slow!=fast)

{

slow=slow->next;

fast=fast->next;

}

Start the loop from the ring inlet

slist* P=slow;

Do

{

p=p->next;

length++;

}while (P!=slow)

return length;

}

To determine the ring in a linked list

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.