From be7b3bf2b8ef2ddc2a66d1df7a0ebf3628f1658d Mon Sep 17 00:00:00 2001 From: Pradyumna Bhat <122141906+pradyumna100903@users.noreply.github.com> Date: Wed, 6 Mar 2024 22:53:28 +0530 Subject: [PATCH 1/9] Create Linked List.c --- March/Linked List Cycle/Linked List.c | 30 +++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 March/Linked List Cycle/Linked List.c diff --git a/March/Linked List Cycle/Linked List.c b/March/Linked List Cycle/Linked List.c new file mode 100644 index 0000000..fe4ac4f --- /dev/null +++ b/March/Linked List Cycle/Linked List.c @@ -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; +} + From 973974ff699bca700d1d22c48711cb6853012b86 Mon Sep 17 00:00:00 2001 From: Pradyumna Bhat <122141906+pradyumna100903@users.noreply.github.com> Date: Wed, 6 Mar 2024 22:55:48 +0530 Subject: [PATCH 2/9] Create Readme.md --- March/Linked List Cycle/Readme.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 March/Linked List Cycle/Readme.md diff --git a/March/Linked List Cycle/Readme.md b/March/Linked List Cycle/Readme.md new file mode 100644 index 0000000..153ecb6 --- /dev/null +++ b/March/Linked List Cycle/Readme.md @@ -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. From 30216fd0bf1cea2e9dfee24af89c423f30061ab0 Mon Sep 17 00:00:00 2001 From: Pradyumna Bhat <122141906+pradyumna100903@users.noreply.github.com> Date: Wed, 6 Mar 2024 22:56:10 +0530 Subject: [PATCH 3/9] Update readme.md --- March/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/March/readme.md b/March/readme.md index ffe5d36..b7f4685 100644 --- a/March/readme.md +++ b/March/readme.md @@ -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) | [Source code](https://github.com/Abiji-2020/Leetcode-2024/tree/main/March/CPP/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) | | [Source Code](https://github.com/pradyumna100903/Leetcode-2024/blob/main/March/Linked%20List%20Cycle/Linked%20List.c) | 7 | | | | | | | 8 | | | | | | | 9 | | | | | | From 8356c76a98d4158fd4a741b0556150d090f070e7 Mon Sep 17 00:00:00 2001 From: Pradyumna Bhat <122141906+pradyumna100903@users.noreply.github.com> Date: Thu, 7 Mar 2024 23:04:15 +0530 Subject: [PATCH 4/9] Update readme.md --- March/readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/March/readme.md b/March/readme.md index 8f30e23..38f9df7 100644 --- a/March/readme.md +++ b/March/readme.md @@ -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) | [Source Code](https://github.com/pradyumna100903/Leetcode-2024/blob/main/March/Linked%20List%20Cycle/Linked%20List.c) +| 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) | | 8 | | | | | | | 9 | | | | | | | 10 | | | | | | From 3bd21364562a867c2031b40b6e7986fd617597e2 Mon Sep 17 00:00:00 2001 From: Pradyumna Bhat <122141906+pradyumna100903@users.noreply.github.com> Date: Thu, 7 Mar 2024 23:04:59 +0530 Subject: [PATCH 5/9] Create Middle of LL.c --- March/C/Middle of Linked List/Middle of LL.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 March/C/Middle of Linked List/Middle of LL.c diff --git a/March/C/Middle of Linked List/Middle of LL.c b/March/C/Middle of Linked List/Middle of LL.c new file mode 100644 index 0000000..d9da072 --- /dev/null +++ b/March/C/Middle of Linked List/Middle of LL.c @@ -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; +} From b78e0869f5b05bb1e8d450e743859521f5a40eef Mon Sep 17 00:00:00 2001 From: Pradyumna Bhat <122141906+pradyumna100903@users.noreply.github.com> Date: Thu, 7 Mar 2024 23:05:57 +0530 Subject: [PATCH 6/9] Create Readme.md --- March/C/Middle of Linked List/Readme.md | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 March/C/Middle of Linked List/Readme.md diff --git a/March/C/Middle of Linked List/Readme.md b/March/C/Middle of Linked List/Readme.md new file mode 100644 index 0000000..23cbb5d --- /dev/null +++ b/March/C/Middle of Linked List/Readme.md @@ -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. From 9734a5a33d662a7432593640d3295b6769603281 Mon Sep 17 00:00:00 2001 From: Pradyumna Bhat <122141906+pradyumna100903@users.noreply.github.com> Date: Thu, 7 Mar 2024 23:07:12 +0530 Subject: [PATCH 7/9] Update readme.md --- March/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/March/readme.md b/March/readme.md index 38f9df7..803e4b8 100644 --- a/March/readme.md +++ b/March/readme.md @@ -16,7 +16,7 @@ | 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) | - | [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) | +| 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 | | | | | | From 8164ffd40966132faa8326c5cdc6d096f258fdc1 Mon Sep 17 00:00:00 2001 From: Pradyumna Bhat <122141906+pradyumna100903@users.noreply.github.com> Date: Thu, 7 Mar 2024 23:31:52 +0530 Subject: [PATCH 8/9] Update Readme.md --- March/C/Middle of Linked List/Readme.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/March/C/Middle of Linked List/Readme.md b/March/C/Middle of Linked List/Readme.md index 23cbb5d..9a2c930 100644 --- a/March/C/Middle of Linked List/Readme.md +++ b/March/C/Middle of Linked List/Readme.md @@ -1,10 +1,10 @@ # Approach -## Initialization: Start with two pointers, fast and slow, both pointing to the head of the list. +1. 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. +2. 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. +3. 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. +4. 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. +5. Return the node pointed to by the slow pointer as the middle node. From 231adb898f88119f3816b83e8c130a3ca2ce0c5c Mon Sep 17 00:00:00 2001 From: Pradyumna Bhat <122141906+pradyumna100903@users.noreply.github.com> Date: Thu, 7 Mar 2024 23:34:04 +0530 Subject: [PATCH 9/9] Update Readme.md --- March/Linked List Cycle/Readme.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/March/Linked List Cycle/Readme.md b/March/Linked List Cycle/Readme.md index 153ecb6..92d164b 100644 --- a/March/Linked List Cycle/Readme.md +++ b/March/Linked List Cycle/Readme.md @@ -1,7 +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. +1.Use two pointers, slow and fast, initially both pointing to the head of the linked list.
+2.Move slow pointer one step at a time and fast pointer two steps at a time.
+3.If there is a cycle, eventually the fast pointer will meet the slow pointer within the cycle.
+4.If there is no cycle, the fast pointer will reach the end of the list.
+5.Return true if fast and slow pointers meet, indicating the presence of a cycle, otherwise return false.