Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ff9b734

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 486b52f commit ff9b734

File tree

5 files changed

+105
-23
lines changed

5 files changed

+105
-23
lines changed

‎141_linked_list_cycle/list_cycle.c‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@ struct ListNode {
99

1010
static bool hasCycle(struct ListNode *head)
1111
{
12-
if (head == NULL || head->next == NULL) {
13-
return false;
14-
}
15-
16-
bool first = true;
17-
struct ListNode *p0, *p1;
18-
for (p0 = head, p1 = head; p1 != NULL && p1->next != NULL; p0 = p0->next, p1 = p1->next->next) {
19-
if (p0 == p1 && !first) {
12+
struct ListNode *fast = head;
13+
struct ListNode *slow = head;
14+
while (fast != NULL && fast->next != NULL) {
15+
slow = slow->next;
16+
fast = fast->next->next;
17+
if (fast == slow) {
2018
return true;
2119
}
22-
first = false;
2320
}
2421

2522
return false;

‎141_linked_list_cycle/list_cycle.cc‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
/**
6+
* Definition for singly-linked list.
7+
* struct ListNode {
8+
* int val;
9+
* ListNode *next;
10+
* ListNode(int x) : val(x), next(NULL) {}
11+
* };
12+
*/
13+
class Solution {
14+
public:
15+
bool hasCycle(ListNode *head) {
16+
struct ListNode *fast = head;
17+
struct ListNode *slow = head;
18+
while (fast != nullptr && fast->next != nullptr) {
19+
slow = slow->next;
20+
fast = fast->next->next;
21+
if (fast == slow) {
22+
return true;
23+
}
24+
}
25+
return false;
26+
}
27+
};

‎142_linked_list_cycle_ii/list_cycle.c‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,19 @@ struct ListNode {
99

1010
static struct ListNode *detectCycle(struct ListNode *head)
1111
{
12-
if (head == NULL || head->next == NULL) {
13-
return false;
14-
}
15-
16-
bool first = true;
17-
struct ListNode *p0, *p1;
18-
for (p0 = head, p1 = head; p1 != NULL && p1->next != NULL; p0 = p0->next, p1 = p1->next->next) {
19-
if (p0 == p1 && !first) {
20-
p0 = head;
21-
while (p0 != p1) {
22-
p0 = p0->next;
23-
p1 = p1->next;
12+
struct ListNode *fast = head;
13+
struct ListNode *slow = head;
14+
while (fast != NULL && fast->next != NULL) {
15+
slow = slow->next;
16+
fast = fast->next->next;
17+
if (fast == slow) {
18+
fast = head;
19+
while (fast != slow) {
20+
fast = fast->next;
21+
slow = slow->next;
2422
}
25-
return p0;
23+
return fast;
2624
}
27-
first = false;
2825
}
2926

3027
return NULL;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
/**
6+
* Definition for singly-linked list.
7+
* struct ListNode {
8+
* int val;
9+
* ListNode *next;
10+
* ListNode(int x) : val(x), next(NULL) {}
11+
* };
12+
*/
13+
class Solution {
14+
public:
15+
ListNode* detectCycle(ListNode *head) {
16+
ListNode *fast = head;
17+
ListNode *slow = head;
18+
while (fast != nullptr && fast->next != nullptr) {
19+
slow = slow->next;
20+
fast = fast->next->next;
21+
if (fast == slow) {
22+
fast = head;
23+
while (fast != slow) {
24+
fast = fast->next;
25+
slow = slow->next;
26+
}
27+
return fast;
28+
}
29+
}
30+
return nullptr;
31+
}
32+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
/**
6+
* Definition for a binary tree node.
7+
* struct TreeNode {
8+
* int val;
9+
* TreeNode *left;
10+
* TreeNode *right;
11+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
12+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
13+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
14+
* };
15+
*/
16+
class Solution {
17+
public:
18+
TreeNode* invertTree(TreeNode* root) {
19+
if (root == NULL) {
20+
return NULL;
21+
}
22+
23+
TreeNode* l = invertTree(root->left);
24+
TreeNode* r = invertTree(root->right);
25+
root->left = r;
26+
root->right = l;
27+
return root;
28+
}
29+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /