Skip to content

Commit

Permalink
141. Linked List Cycle using slow and fast pointer technique
Browse files Browse the repository at this point in the history
  • Loading branch information
erickcm2k committed Dec 4, 2020
1 parent ec485d2 commit 1b380b1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions leetcode/linked-list/141-Linked-List-Cycle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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 *slow = head;
ListNode *fast = head;

while (slow && fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
if (slow == fast)
{
return true;
}
}
return false;
}
};
Binary file removed leetcode/linked-list/a.out
Binary file not shown.

0 comments on commit 1b380b1

Please sign in to comment.