(Leetcode 141/142) Linked List Cycle

Source: Internet
Author: User
Tags set set

1, Given a linked list, determine if it has a cycle in it.

2, Given a linked list, return the node where the cycle begins. If There is no cycle, return null .

3, Given a linked list, return the length of the cycle, if there is no cycle, return 0;

Title Requirements:

1, to a linked list, to determine whether it exists ring;

2, give a list, if it exists ring, please return to the beginning of the ring, otherwise, return null;

3, give a list, if it exists ring, please return the length of the ring, otherwise, return 0;

Problem-Solving ideas: 1, to determine whether there is a ring?

Method 1: Add a set container, traverse the linked list, and then add each node to the set set, if the node already exists in the collection, indicating that there is a ring, if there are no duplicate nodes after the traversal, then there is no ring.

Method 2: No additional space required, set two pointers p1,p2, start from the beginning, one step, p1=p1->next; A walk two steps, P2=p2->next->next, if there is a ring, then two pointers will surely meet If the fast-moving pointer p2 reaches the end point, it indicates no ring.

2, how to find the beginning of the ring and the length of the ring?

Set the starting distance of the chain list is a, the ring length is N, when the P1 and P2 meet, the distance from the point of encounter points is B, at this time B has been around the ring go K Circle, then

P1 Walk the distance for a+b;

P2 speed of p1 twice times, p2 Walk Distance is (a+b);

P2 Walking distance is a+b+k*n=2* (a+b), thus a+b=k*n

That is, when P1 walk a step, p2 walk (k*n-b) step, when K=1, then for (n-b) step;

So how to find the starting point of the ring? P1 pull back to start again, P2 from meet point continue to walk, p1,p2 each step, a step, p1 arrive at the beginning, p2 also just to the beginning of the circle.

how to find the length of the ring? after the encounter, P2 again walk a circle and the statistic length is the circle length.

Reference code: 1, to determine whether there is a ring?
/** Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x) : Val (x), Next (NULL) {}}; */classSolution { Public:    BOOLHascycle (ListNode *head) {       Set<ListNode*>psets;  for(ListNode *p=head;p!=null;p=p->next) {           if(Psets.find (p)! =psets.end ())return true;       Psets.insert (P); }       return false; }};

/** Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x) : Val (x), Next (NULL) {}}; */classSolution { Public:    BOOLHascycle (ListNode *head) {//if (head==null | | head->next==null)//return false;ListNode *p1=Head; ListNode*p2=Head;  while(P2 && p2->next) {P1=p1->Next; P2=p2->next->Next; if(p1==p2)return true; }        return false; }};

2. The starting point of seeking the ring
/** Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x) : Val (x), Next (NULL) {}}; */classSolution { Public: ListNode*detectcycle (ListNode *head) {ListNode*p1,*P2; P1=Head; P2=Head;  while(P2 && p2->next) {P1=p1->Next; P2=p2->next->Next; if(p1==p2) {                //P1 points back to head,p2 still stand where they has met//P1,P2 Take the same steps//When they meet again, that ' s where the cycle startsp1=Head;  while(p1!=p2) {P1=p1->Next; P2=p2->Next; }                returnP1; }        }        returnNULL; }};
3, the length of the ring to seek
/** Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x) : Val (x), Next (NULL) {}}; */classSolution { Public: ListNode*detectcycle (ListNode *head) {ListNode*p1,*P2; P1=Head; P2=Head;  while(P2 && p2->next) {P1=p1->Next; P2=p2->next->Next; if(p1==p2) {                //if P2 cycles around,then the length of the cycle can be countedListNode *p=p2->Next; intLength=1;  while(p!=p2) {Length++; P2=p2->Next; }                returnlength; }        }        return 0; }};

(Leetcode 141/142) Linked List Cycle

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.