Compare commits
2 Commits
f560e933f4
...
f3e576da31
Author | SHA1 | Date | |
---|---|---|---|
f3e576da31 | |||
90cce31c8e |
13
136_single_number/main.cpp
Normal file
13
136_single_number/main.cpp
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int singleNumber(vector<int>& nums) {
|
||||||
|
int result = 0;
|
||||||
|
for (auto num : nums) {
|
||||||
|
result ^= num;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
38
141_linked_list_cycle/main.cpp
Normal file
38
141_linked_list_cycle/main.cpp
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
struct ListNode {
|
||||||
|
int val;
|
||||||
|
ListNode *next;
|
||||||
|
ListNode(int x) : val(x), next(NULL) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
bool hasCycle(ListNode *head) {
|
||||||
|
if (!head) return false;
|
||||||
|
|
||||||
|
ListNode *slowNode = head;
|
||||||
|
ListNode *fastNode = head->next;
|
||||||
|
|
||||||
|
while (fastNode && fastNode->next) {
|
||||||
|
if (slowNode == fastNode && fastNode) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
slowNode = slowNode->next;
|
||||||
|
fastNode = fastNode->next->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
ListNode *head = new ListNode(1);
|
||||||
|
head->next = new ListNode(2);
|
||||||
|
|
||||||
|
Solution().hasCycle(head);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user