赞
踩
题目:
题解:
- struct ListNode* detectCycle(struct ListNode* head) {
- struct ListNode *slow = head, *fast = head;
- while (fast != NULL) {
- slow = slow->next;
- if (fast->next == NULL) {
- return NULL;
- }
- fast = fast->next->next;
- if (fast == slow) {
- struct ListNode* ptr = head;
- while (ptr != slow) {
- ptr = ptr->next;
- slow = slow->next;
- }
- return ptr;
- }
- }
- return NULL;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。