This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Initialize slow & fast pointers | |
ListNode* slow = head; | |
ListNode* fast = head; | |
/** | |
* Change this condition to fit specific problem. | |
* Attention: remember to avoid null-pointer error | |
**/ | |
while (slow && fast && fast->next) { | |
slow = slow->next; // move slow pointer one step each time | |
fast = fast->next->next; // move fast pointer two steps each time | |
if (slow == fast) { // change this condition to fit specific problem | |
return true; | |
} | |
} | |
return false; // change return value to fit specific problem |
No comments:
Post a Comment