Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day 06 #188

Merged
merged 10 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions March/C/Middle of Linked List/Middle of LL.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* middleNode(struct ListNode* head) {
struct ListNode* slow = head;
struct ListNode* fast = head;

while(fast && fast->next){
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
10 changes: 10 additions & 0 deletions March/C/Middle of Linked List/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Approach
## Initialization: Start with two pointers, fast and slow, both pointing to the head of the list.

## Traversal: Move the fast pointer two steps at a time and the slow pointer one step at a time. This ensures that when the fast pointer reaches the end of the list, the slow pointer will be at the middle node.

## Find the Middle Node: After traversal, the slow pointer will be at the middle node of the list.

## Edge Case Handling: Check if the list is empty or contains only one node. In such cases, the middle node is the head itself.

## Return: Return the node pointed to by the slow pointer as the middle node.
30 changes: 30 additions & 0 deletions March/Linked List Cycle/Linked List.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
bool hasCycle(struct ListNode *head) {
struct ListNode*p=head,*q=head;
if(head==NULL)
return false;
if(head->next==NULL)
return false;
do
{
p=p->next;
q=q->next;
if(q)
q=q->next;
else
q=NULL;
}
while(p!=NULL && q!=NULL && p!=q);

if(p==q)
return true;
else
return false;
}

7 changes: 7 additions & 0 deletions March/Linked List Cycle/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Approach:

Traverse linked list using two pointers.

Move one pointer(p) by one and another pointer(q) by two.

If these pointers meet at the same node then there is a loop. If pointers do not meet then linked list doesn’t have a loop.
4 changes: 2 additions & 2 deletions March/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
| 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) | [Source code](https://github.com/Abiji-2020/Leetcode-2024/tree/main/March/CPP/Linked%20List%20cycle) | - | |
| 7 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Source Code](https://github.com/dhruvabhat24/Leetcode-2024/tree/main/March/Java/Middle%20of%20the%20Linked%20List) |[Source code](https://github.com/Abiji-2020/Leetcode-2024/tree/main/March/CPP/Middle%20of%20Linked%20List) | [Source code](https://github.com/GajananShenvi/Leetcode-2024/tree/main/March/Python/Middle%20of%20the%20Linked%20List) | |
| 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) | - | [Source Code](https://github.com/pradyumna100903/Leetcode-2024/blob/main/March/Linked%20List%20Cycle/Linked%20List.c)
| 7 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Source Code](https://github.com/dhruvabhat24/Leetcode-2024/tree/main/March/Java/Middle%20of%20the%20Linked%20List) |[Source code](https://github.com/Abiji-2020/Leetcode-2024/tree/main/March/CPP/Middle%20of%20Linked%20List) | [Source code](https://github.com/GajananShenvi/Leetcode-2024/tree/main/March/Python/Middle%20of%20the%20Linked%20List) | [Source Code](https://github.com/pradyumna100903/Leetcode-2024/blob/main/March/C/Middle%20of%20Linked%20List/Middle%20of%20LL.c)
| 8 | | | | | |
| 9 | | | | | |
| 10 | | | | | |
Expand Down