Skip to content

Commit f6c56f7

Browse files
committed
Implement linked-list-cycle-ii
https://leetcode.com/problems/linked-list-cycle-ii time: 16ms time-rank: 21.04% time-complexity: O(N) space: 10MB space-rank: 9.36% space-complexity: O(N) Fixes #228 Signed-off-by: Christopher Friedt <[email protected]>
1 parent 52fa04b commit f6c56f7

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

linked-list-cycle-ii-test.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2018 Christopher Friedt
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#include <vector>
26+
27+
#include <gtest/gtest.h>
28+
29+
#include "util/ListNode.cpp"
30+
31+
#include "linked-list-cycle-ii.cpp"
32+
33+
TEST(LinkedListCycleIi, Test_3_2_0_n4__1) {
34+
vector<ListNode> nodes = {
35+
ListNode(3),
36+
ListNode(2),
37+
ListNode(0),
38+
ListNode(-4),
39+
};
40+
nodes[0].next = &nodes[1];
41+
nodes[1].next = &nodes[2];
42+
nodes[2].next = &nodes[3];
43+
nodes[3].next = &nodes[1];
44+
EXPECT_EQ(&nodes[1], Solution().detectCycle(&nodes[0]));
45+
}
46+
47+
TEST(LinkedListCycleIi, Test_1_2__0) {
48+
vector<ListNode> nodes = {
49+
ListNode(1),
50+
ListNode(2),
51+
};
52+
nodes[0].next = &nodes[1];
53+
nodes[1].next = &nodes[0];
54+
EXPECT_EQ(&nodes[0], Solution().detectCycle(&nodes[0]));
55+
}
56+
57+
TEST(LinkedListCycleIi, Test_1_n1) {
58+
vector<ListNode> nodes = {
59+
ListNode(1),
60+
};
61+
nodes[0].val = 1;
62+
nodes[0].next = NULL;
63+
EXPECT_EQ(NULL, Solution().detectCycle(&nodes[0]));
64+
}

linked-list-cycle-ii.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2018 Christopher Friedt
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#include <unordered_set>
26+
27+
using namespace std;
28+
29+
// https://leetcode.com/problems/linked-list-cycle-ii
30+
31+
class Solution {
32+
public:
33+
ListNode *detectCycle(ListNode *head) {
34+
unordered_set<ListNode *> visited;
35+
ListNode *it;
36+
for (it = head; it != NULL; it = it->next) {
37+
if (visited.end() != visited.find(it)) {
38+
return it;
39+
}
40+
visited.insert(it);
41+
}
42+
return it;
43+
}
44+
45+
/*
46+
// Recursive solution (obviously better to use iterative)
47+
ListNode *helper(unordered_set<ListNode *> &visited, ListNode *n) {
48+
if (NULL == n) {
49+
return NULL;
50+
}
51+
if (visited.end() != visited.find(n)) {
52+
return n;
53+
}
54+
visited.insert(n);
55+
return helper(visited, n->next);
56+
}
57+
*/
58+
};

0 commit comments

Comments
 (0)