Skip to content

Commit 5bec96c

Browse files
committed
Create Linked_List_Cycle.cc
1 parent 6fbd9b3 commit 5bec96c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Linked_List_Cycle.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
bool hasCycle(ListNode *head) {
12+
// IMPORTANT: Please reset any member data you declared, as
13+
// the same Solution instance will be reused for each test case.
14+
ListNode *fast = head, *slow = head;
15+
while (fast && fast->next) {
16+
fast = fast->next->next;
17+
slow = slow->next;
18+
if (fast == slow)
19+
return true;
20+
}
21+
return false;
22+
}
23+
};

0 commit comments

Comments
 (0)