Skip to content

Commit

Permalink
Merge pull request #187 from Abiji-2020/main
Browse files Browse the repository at this point in the history
Day 6
  • Loading branch information
dhruvabhat24 authored Mar 6, 2024
2 parents df84971 + f3355ea commit 0085697
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
22 changes: 22 additions & 0 deletions March/CPP/Linked List cycle/Linked List Cycle code.cpp
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;

}
};
34 changes: 34 additions & 0 deletions March/CPP/Linked List cycle/Readme.md
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`.
2 changes: 1 addition & 1 deletion March/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
| 3 | [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/?envType=daily-question&envId=2024-03-03) | [Source Code](https://github.com/dhruvabhat24/Leetcode-2024/tree/main/March/Java/Remove%20Nth%20Node%20From%20End%20of%20List) |[Source code](https://github.com/Abiji-2020/Leetcode-2024/tree/main/March/CPP/Remove%20Nth%20Node%20from%20End%20of%20List) | - | [Source Code](https://github.com/pradyumna100903/Leetcode-2024/blob/main/March/C/Remove%20Nth%20Node%20From%20End%20of%20List/Remove%20Nth%20node.c)
| 4 | [Bag of Tokens](https://leetcode.com/problems/bag-of-tokens/) | [Source Code](https://github.com/dhruvabhat24/Leetcode-2024/tree/main/March/Java/Bag%20of%20Tokens) |[Source code](https://github.com/Abiji-2020/Leetcode-2024/tree/main/March/CPP/Bag%20of%20Tokens) | - | [Source Code](https://github.com/pradyumna100903/Leetcode-2024/blob/main/March/C/Bag%20of%20Tokens/Bag%20of%20Tokens.c)
| 5 | [Minimum Length of String After Deleting Similar Ends](https://leetcode.com/problems/minimum-length-of-string-after-deleting-similar-ends/) |[Source Code](https://github.com/dhruvabhat24/Leetcode-2024/tree/main/March/Java/Minimum%20Length%20of%20String%20After%20Deleting%20Similar%20Ends) | [Source code](https://github.com/Abiji-2020/Leetcode-2024/tree/main/March/CPP/Minimum%20Length%20of%20String%20After%20Deleting%20Similar%20Elements) | - | - |
| 6 | [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) |[Source Code](https://github.com/dhruvabhat24/Leetcode-2024/tree/main/March/Java/Linked%20List%20Cycle) | | | |
| 6 | [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) |[Source Code](https://github.com/dhruvabhat24/Leetcode-2024/tree/main/March/Java/Linked%20List%20Cycle) | [Source code](https://github.com/Abiji-2020/Leetcode-2024/tree/main/March/CPP/Linked%20List%20cycle) | | |
| 7 | | | | | |
| 8 | | | | | |
| 9 | | | | | |
Expand Down

0 comments on commit 0085697

Please sign in to comment.