Skip to content

Commit 0a69fde

Browse files
committed
Create Linked List CycleI
1 parent 9f6c787 commit 0a69fde

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

List/Linked List CycleI

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// 设置两个指针一块一慢
2+
class Solution {
3+
public:
4+
bool hasCycle(ListNode *head) {
5+
ListNode *slow = head,*fast = head;
6+
while (slow && fast->next)
7+
{
8+
slow = slow->next;
9+
fast = fast->next->next;
10+
if(slow = fast)
11+
return true;
12+
}
13+
return false;
14+
15+
16+
}
17+
};
18+
// 自己的方法。。。更改了原来的数据表。。所以。。有点不完美
19+
class Solution {
20+
public:
21+
bool hasCycle(ListNode *head) {
22+
ListNode *p = head;
23+
while (p)
24+
{
25+
if(p->val != INT_MIN)
26+
p->val = INT_MIN;
27+
else
28+
return true;
29+
p = p->next;
30+
}
31+
return false;
32+
33+
34+
}
35+
};

0 commit comments

Comments
 (0)