mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-18 00:57:17 +00:00
Initial commit
This commit is contained in:
50
Easy/linked-list-cycle/solution.cpp
Normal file
50
Easy/linked-list-cycle/solution.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <iostream>
|
||||
#include <unordered_map>
|
||||
|
||||
struct ListNode {
|
||||
int val;
|
||||
ListNode *next;
|
||||
ListNode(int x) : val(x), next(NULL) {}
|
||||
};
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
// bool hasCycle(ListNode *head) {
|
||||
// std::unordered_map<ListNode *, int> hmap;
|
||||
// ListNode *curr = head;
|
||||
// while (curr != NULL) {
|
||||
// if (++hmap[curr] == 2) {
|
||||
// return true;
|
||||
// } else {
|
||||
// curr = curr->next;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return false;
|
||||
// }
|
||||
|
||||
bool hasCycle(ListNode *head) {
|
||||
if (head == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ListNode *slow = head, *fast = head;
|
||||
int taken = 0, limit = 2;
|
||||
|
||||
while (fast->next != NULL) {
|
||||
fast = fast->next;
|
||||
++taken;
|
||||
if (slow == fast) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (taken == limit) {
|
||||
taken = 0;
|
||||
limit *= 2;
|
||||
slow = fast;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user