Where are the fast and fast pointers pointing to? This problem is often not good for people who have just come into contact with this skill. The following will be a single-linked list For example, say a little bit of my personal understanding.
1) A more general wording
if (head = = NULL | | head->next = = NULL) return true; ListNode *fast = head, *slow = head, *prev = null;while (Fast && fast->next) {prev = Slow;slow = slow->next; Fast = Fast->next->next;} </span>
End condition analysis for While loop:
Fast = = NULL even (number of list elements) ******|s***** slow the first element to the second half
Fast->next = = NULL odd *****|s|***** slow point to center position
Sometimes you need to point to the last position of the first part, such as a split list. This requires adding a prev record to the previous element of slow. Is there no need to add prev?
We can deform this method slightly.
2) a slight distortion
if (head = = NULL | | head->next = = NULL) return true; ListNode *fast = head, *slow= head;while (fast->next && fast->next->next) {slow = Slow->next;fast = FAs T->next->next;} </span>
End condition Analysis for While loop:
Fast->next = = NULL Odd ****s|*|*****
Fast->next->next = = NULL even ****s|*****
Slow always points to the last element in the first part.
Compared to the first method, this method detects more than one depth in the while loop, thus ending the loop one step ahead. That is, the slow pointer took a step less.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Where does the speed pointer point to exactly?