This article discusses a list of links related to the topic, I currently encounter a total of 3 kinds of topics.
1. Determine if a linked list has a ring (Leetcode related topic: https://leetcode.com/problems/linked-list-cycle/description/)
Set two pointers, the initial values are pointing to the head, a fast and slow, slow each step forward, fast every two steps, if the list exists ring, two pointers will meet. If fast goes to the tail, it is a chain-free list.
The code is as follows:
/** Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x) : Val (x), Next (NULL) {}}; */classSolution { Public: BOOLHascycle (ListNode *head) {ListNode*fast =Head; ListNode*slow =Head; while(Fast && fast->next) {Fast= fast->next->Next; Slow= slow->Next; if(Slow = =fast) { return true; } } return false; }};
2. Determine the position of a ring in a linked list (Leetcode Related topics: https://leetcode.com/problems/linked-list-cycle-ii/description/)
is also used to a fast a slow two pointers, borrowed a graph on the Internet:
Two hands moving clockwise, fast a two-step, slow one step at a time, assuming that two pointers for the first time in the Z meet, slow traversed by the distance for the A+b,fast traversed by the distance of a+b+c+b,2 (A + b) = = a + B + C + B, to get a = = C, so this time the fast pointer Put back the starting point and take two steps to take one step at a time. At this point, fast and slow start at the same time, because their journey and speed are the same, so they will meet at the beginning of the ring.
The code is as follows:
/** 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*slow =Head; ListNode*fast =Head; while(true) { if(fast = = NULL | | fast->next = = NULL)returnNULL; Else{Fast= fast->next->Next; Slow= slow->Next; if(Fast = = slow) Break; }} Fast=Head; while(Fast! =slow) {Slow= slow->Next; Fast= fast->Next; } returnslow; }};
3. Find the intersection of two linked lists (Leetcode Related topics: https://leetcode.com/problems/intersection-of-two-linked-lists/description/)
Although it does not seem to have anything to do with the chain of linked lists, the problem can be done in this way. We can first connect the head and tail of one of the rings, and then follow the previous question, we can find the location of the intersection. But remember to restore everything, that is, to untie the ring.
The code is as follows:
/** Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x) : Val (x), Next (NULL) {}}; */classSolution { Public: ListNode*getintersectionnode (ListNode *heada, ListNode *headb) { if(Heada = = NULL | | headb = = NULL)returnNULL; ListNode*copya =Heada; ListNode*copyb =headb; while(Heada->next! =NULL) {Heada= heada->Next; } ListNode*recover =Heada; Heada->next =Copya; ListNode*fast =headb; ListNode*slow =headb; while(true) { if(!fast | |!fast->next) {Recover->next =NULL; Heada=Copya; HEADB=Copyb; returnNULL; } Fast= fast->next->Next; Slow= slow->Next; if(Fast = = slow) Break; } Fast=headb; while(Fast! =slow) {Slow= slow->Next; Fast= fast->Next; } Recover->next =NULL; Heada=Copya; HEADB=Copyb; returnslow; }};
4. Other ...
Other types of topics I can think of:
To find the length of the ring: in the premise of 2, starting from the beginning of the ring, a pointer does not move, a pointer walk, the length of the statistics walk, the length of two of the hands when the re-encounter is the length of the ring
Unlock the Ring: The principle is actually the same
Therefore, the key to this type of problem is to use a fast and slow two pointers, and then to understand the way to find the starting point of the ring, other deformation problems will be solved.
Link list related topics and algorithms [Leetcode]