File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* *
2
+ * @Author: Chacha
3
+ * @Date: 2019-01-14 15:24:40
4
+ * @Last Modified by: Chacha
5
+ * @Last Modified time: 2019-01-14 15:37:33
6
+ */
7
+
8
+
9
+ #include < iostream>
10
+ #include < string>
11
+ using namespace std ;
12
+
13
+ /* *
14
+ * Definition for singly-linked list(单向链表)
15
+ * Source: https://zh.wikipedia.org/wiki/链表
16
+ */
17
+ struct ListNode {
18
+ int val;
19
+ ListNode *next;
20
+ ListNode (int x) : val(x), next(NULL ) {}
21
+ };
22
+
23
+ class Solution {
24
+ public:
25
+ /* *
26
+ * Given a linked list, determine if it has a cycle in it.
27
+ * Example: Given -21->10->4->5, tail connects to node index 1, return true
28
+ * Source: https://leetcode.com/problems/linked-list-cycle/
29
+ */
30
+ bool hasCycle (ListNode* head) {
31
+ if (head == NULL || head->next == NULL ) return false ;
32
+
33
+ ListNode* slow = head;
34
+ ListNode* fast = head->next ;
35
+
36
+ while (fast != NULL && fast->next != NULL ) {
37
+ fast = fast->next ->next ;
38
+ slow = slow->next ;
39
+
40
+ if (slow == fast) return true ;
41
+ }
42
+
43
+ return false ;
44
+ }
45
+ };
46
+
47
+ int main () {
48
+ return 0 ;
49
+ }
You can’t perform that action at this time.
0 commit comments