We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0a69fde commit ec3cf3aCopy full SHA for ec3cf3a
List/Linked List CycleI
@@ -33,3 +33,30 @@
33
34
}
35
};
36
+ // 数学
37
+ class Solution {
38
+ public:
39
+ ListNode *detectCycle(ListNode *head) {
40
+ ListNode *slow = head,*fast = head;
41
+ while (fast && fast->next) // 防止空指针异常
42
+ {
43
+ slow = slow ->next;
44
+ fast = fast->next->next;
45
+ if(slow == fast)
46
+ break;
47
+ }
48
+ if(fast==nullptr || fast->next == nullptr)
49
+ return NULL;
50
+ else
51
52
+ ListNode *second = head;
53
+ while (slow != second)
54
55
+ slow=slow->next;
56
+ second=second->next;
57
+
58
59
+ return second;
60
61
62
+ };
0 commit comments