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 6fbd9b3 commit 5bec96cCopy full SHA for 5bec96c
Linked_List_Cycle.cc
@@ -0,0 +1,23 @@
1
+/**
2
+ * Definition for singly-linked list.
3
+ * struct ListNode {
4
+ * int val;
5
+ * ListNode *next;
6
+ * ListNode(int x) : val(x), next(NULL) {}
7
+ * };
8
+ */
9
+class Solution {
10
+public:
11
+ bool hasCycle(ListNode *head) {
12
+ // IMPORTANT: Please reset any member data you declared, as
13
+ // the same Solution instance will be reused for each test case.
14
+ ListNode *fast = head, *slow = head;
15
+ while (fast && fast->next) {
16
+ fast = fast->next->next;
17
+ slow = slow->next;
18
+ if (fast == slow)
19
+ return true;
20
+ }
21
+ return false;
22
23
+};
0 commit comments