-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from Abiji-2020/main
Day 6
- Loading branch information
Showing
3 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* 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* fast=head; | ||
ListNode* slow = head; | ||
while(fast != nullptr && fast->next != nullptr){ | ||
fast= fast->next->next; | ||
slow = slow->next; | ||
if(fast== slow) return true; | ||
} | ||
return false; | ||
|
||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
## Class: Solution | ||
|
||
This class contains a method `hasCycle()` to determine whether a linked list has a cycle. | ||
|
||
### Method: `hasCycle` | ||
|
||
Determines whether a linked list has a cycle. | ||
|
||
#### Parameters | ||
|
||
- `head`: A pointer to the head of the linked list. | ||
|
||
#### Returns | ||
|
||
- A boolean value indicating whether the linked list has a cycle. | ||
|
||
#### Approach | ||
|
||
1. **Two Pointers:** | ||
- Initialize two pointers `slow` and `fast` to the head of the linked list. | ||
- Traverse the linked list using two pointers: | ||
- `slow` moves one step at a time. | ||
- `fast` moves two steps at a time. | ||
- If there is a cycle, eventually, `fast` will meet `slow` somewhere in the linked list. | ||
|
||
2. **Detecting Cycle:** | ||
- While traversing, if `fast` becomes nullptr or `fast->next` becomes nullptr, it means there is no cycle, and the method returns false. | ||
- If at any point `fast` becomes equal to `slow`, it indicates a cycle in the linked list, and the method returns true. | ||
|
||
#### Time Complexity | ||
- The time complexity of this method is O(N), where N is the number of nodes in the linked list. The method traverses the linked list once with two pointers, `slow` and `fast`. | ||
|
||
#### Space Complexity | ||
- The space complexity of this method is O(1) because it only uses a constant amount of extra space for the two pointers `slow` and `fast`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters