-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list_cycle.cpp
More file actions
46 lines (39 loc) 路 1.3 KB
/
Copy pathlinked_list_cycle.cpp
File metadata and controls
46 lines (39 loc) 路 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// https://leetcode.com/problems/linked-list-cycle/
// O(N^2) solution. The idea: use two pointers. The goal is to detect a cycle
// by 'removing' each edge, while keeping track of the removed edge. The
// intuition here is that if an edge is removed, the connected node to the
// removed edge can still be reached if there is a cycle.
// O(N) solution can be attained using the simple Floyd's Tortoise and Hare
// algorithm.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode* tmp = head;
ListNode* curr_node = head;
if (curr_node == NULL) return false;
ListNode * target_node = head->next;
if (target_node == NULL) return false;
while(target_node != NULL) {
tmp = head;
while(tmp != NULL) {
if (tmp == target_node) return true;
if (tmp == curr_node) {
tmp = NULL;
} else {
tmp = tmp->next;
}
}
curr_node = target_node;
target_node = target_node->next;
}
return false;
}
};