From 58792cbf9257448b6ea74e0e7723f3a7f80969aa Mon Sep 17 00:00:00 2001 From: Kavaljeet Singh Date: Fri, 31 Oct 2025 11:45:26 +0530 Subject: [PATCH 1/5] Added solution in cpp Determine if a given linked list contains a cycle.- Hacktoberfest2025 --- .vscode/c_cpp_properties.json | 18 ++++ .vscode/launch.json | 24 +++++ .vscode/settings.json | 59 ++++++++++ dsa_in_cpp/linkedlist/Linkedlistcycle.cpp | 126 ++++++++++++++++++++++ 4 files changed, 227 insertions(+) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 dsa_in_cpp/linkedlist/Linkedlistcycle.cpp diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..5400b25 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "windows-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "C:/gcc-13.2.0-no-debug/bin/gcc.exe", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "windows-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..3f17648 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": true, + "cwd": "c:/Users/91760/Hacktoberfest-Html_CSS_JS/dsa_in_cpp/linkedlist", + "program": "c:/Users/91760/Hacktoberfest-Html_CSS_JS/dsa_in_cpp/linkedlist/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..bb879da --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/dsa_in_cpp/linkedlist/Linkedlistcycle.cpp b/dsa_in_cpp/linkedlist/Linkedlistcycle.cpp new file mode 100644 index 0000000..114777f --- /dev/null +++ b/dsa_in_cpp/linkedlist/Linkedlistcycle.cpp @@ -0,0 +1,126 @@ +/* + Problem: + Given the head of a linked list, determine if the list contains a cycle. + If a cycle exists, return the node where the cycle begins. + If no cycle exists, return nullptr. + + A cycle occurs when a node's `next` pointer points back to a previous node, + forming a loop. + + Approach: + - Use Floyd’s Cycle Detection Algorithm (Tortoise and Hare method): + * Use two pointers — slow and fast. + * Move slow by one step and fast by two steps. + * If they ever meet, a cycle exists. + - To find the **starting node** of the cycle: + * Move one pointer to the head. + * Move both one step at a time — the node where they meet is the cycle start. + + Time Complexity: + - O(n): each node is visited at most twice. + Space Complexity: + - O(1): only uses two pointers, no extra memory. + + Example: + Input: head = [3,2,0,-4], pos = 1 (tail connects to node at index 1) + Output: Node with value 2 (start of the cycle) +*/ + +#include +using namespace std; + +// Definition for singly-linked list +struct ListNode { + int val; + ListNode *next; + ListNode(int x) : val(x), next(nullptr) {} +}; + +// --------------------------- +// Solution Class +// --------------------------- +class Solution { +public: + ListNode *detectCycle(ListNode *head) { + if (head == nullptr) return nullptr; + + ListNode *slow = head; + ListNode *fast = head; + + // Phase 1: Detect if a cycle exists + while (fast != nullptr && fast->next != nullptr) { + slow = slow->next; // move 1 step + fast = fast->next->next; // move 2 steps + + if (slow == fast) { // cycle detected // --------------------------- + // Phase 2: Find the start of the cycle + + slow = head; + while (slow != fast) { + slow = slow->next; + fast = fast->next; + } + return slow; // starting node of cycle + } + } + + return nullptr; // no cycle found + } +}; + + +// Helper to create linked list + +ListNode* createList(vector vals) { + if (vals.empty()) return nullptr; + ListNode* head = new ListNode(vals[0]); + ListNode* curr = head; + for (int i = 1; i < vals.size(); i++) { + curr->next = new ListNode(vals[i]); + curr = curr->next; + } + return head; +} + + +// Helper to create a cycle in the list +// pos = index where tail connects + +void createCycle(ListNode* head, int pos) { + if (pos == -1) return; // no cycle + ListNode* tail = head; + ListNode* cycleNode = nullptr; + int index = 0; + + while (tail->next != nullptr) { + if (index == pos) cycleNode = tail; + tail = tail->next; + index++; + } + + if (cycleNode != nullptr) + tail->next = cycleNode; // create cycle +} + + +// Main function to test the logic + +int main() { + vector vals = {3, 2, 0, -4}; + int pos = 1; // position to form cycle (connect last node to node at index 1) + + ListNode* head = createList(vals); + createCycle(head, pos); + + Solution sol; + ListNode* cycleNode = sol.detectCycle(head); + + if (cycleNode != nullptr) + cout << "Cycle detected at node with value: " << cycleNode->val << endl; + else + cout << "No cycle detected." << endl; + + return 0; +} + + From 94126cde4b242cc83d99fa0320d9a3d14ea9d450 Mon Sep 17 00:00:00 2001 From: Kavaljeet Singh Date: Fri, 31 Oct 2025 11:51:07 +0530 Subject: [PATCH 2/5] Updated Contributer.md- Hacktoberfest2025 --- Contributor.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Contributor.md b/Contributor.md index 5d22f63..b802183 100644 --- a/Contributor.md +++ b/Contributor.md @@ -256,3 +256,7 @@ Added solution for custom range slider 45. Niranjan patil | [LinkedIn](https://www.linkedin.com/in/niranjan-patil-8512a1374) | [GitHub](https://github.com/niranjanpatil1010) - Added Solution for Leetcode Q35 search insert position -added solution for median of two sorted array + +46. Kavaljeet Singh Ahuja | [LinkedIn](https://www.linkedin.com/in/kavaljeet-singh-a76383334/) | [GitHub](https://github.com/kavaljeetsingh-dev) + - Added solution for LeetCode Q142 Detect Cycle in Linked List + - Added main function and detailed comments for better understanding From 460b11ba1fd40110ebecc6d5fa9677f8cb8d0d7a Mon Sep 17 00:00:00 2001 From: Kavaljeet Singh Date: Fri, 31 Oct 2025 20:37:55 +0530 Subject: [PATCH 3/5] Delete .vscode/c_cpp_properties.json --- .vscode/c_cpp_properties.json | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 .vscode/c_cpp_properties.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json deleted file mode 100644 index 5400b25..0000000 --- a/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "configurations": [ - { - "name": "windows-gcc-x64", - "includePath": [ - "${workspaceFolder}/**" - ], - "compilerPath": "C:/gcc-13.2.0-no-debug/bin/gcc.exe", - "cStandard": "${default}", - "cppStandard": "${default}", - "intelliSenseMode": "windows-gcc-x64", - "compilerArgs": [ - "" - ] - } - ], - "version": 4 -} \ No newline at end of file From e2eb9357d3c988a4a521a8d8ba61bbb94f587d08 Mon Sep 17 00:00:00 2001 From: Kavaljeet Singh Date: Fri, 31 Oct 2025 20:39:50 +0530 Subject: [PATCH 4/5] Delete .vscode/launch.json --- .vscode/launch.json | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 3f17648..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - { - "name": "C/C++ Runner: Debug Session", - "type": "cppdbg", - "request": "launch", - "args": [], - "stopAtEntry": false, - "externalConsole": true, - "cwd": "c:/Users/91760/Hacktoberfest-Html_CSS_JS/dsa_in_cpp/linkedlist", - "program": "c:/Users/91760/Hacktoberfest-Html_CSS_JS/dsa_in_cpp/linkedlist/build/Debug/outDebug", - "MIMode": "gdb", - "miDebuggerPath": "gdb", - "setupCommands": [ - { - "description": "Enable pretty-printing for gdb", - "text": "-enable-pretty-printing", - "ignoreFailures": true - } - ] - } - ] -} \ No newline at end of file From ed0c3b24b3e22c8a3ee5a22b592fd0f910d3ac1d Mon Sep 17 00:00:00 2001 From: Kavaljeet Singh Date: Fri, 31 Oct 2025 20:40:18 +0530 Subject: [PATCH 5/5] Delete .vscode/settings.json --- .vscode/settings.json | 59 ------------------------------------------- 1 file changed, 59 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index bb879da..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,59 +0,0 @@ -{ - "C_Cpp_Runner.cCompilerPath": "gcc", - "C_Cpp_Runner.cppCompilerPath": "g++", - "C_Cpp_Runner.debuggerPath": "gdb", - "C_Cpp_Runner.cStandard": "", - "C_Cpp_Runner.cppStandard": "", - "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat", - "C_Cpp_Runner.useMsvc": false, - "C_Cpp_Runner.warnings": [ - "-Wall", - "-Wextra", - "-Wpedantic", - "-Wshadow", - "-Wformat=2", - "-Wcast-align", - "-Wconversion", - "-Wsign-conversion", - "-Wnull-dereference" - ], - "C_Cpp_Runner.msvcWarnings": [ - "/W4", - "/permissive-", - "/w14242", - "/w14287", - "/w14296", - "/w14311", - "/w14826", - "/w44062", - "/w44242", - "/w14905", - "/w14906", - "/w14263", - "/w44265", - "/w14928" - ], - "C_Cpp_Runner.enableWarnings": true, - "C_Cpp_Runner.warningsAsError": false, - "C_Cpp_Runner.compilerArgs": [], - "C_Cpp_Runner.linkerArgs": [], - "C_Cpp_Runner.includePaths": [], - "C_Cpp_Runner.includeSearch": [ - "*", - "**/*" - ], - "C_Cpp_Runner.excludeSearch": [ - "**/build", - "**/build/**", - "**/.*", - "**/.*/**", - "**/.vscode", - "**/.vscode/**" - ], - "C_Cpp_Runner.useAddressSanitizer": false, - "C_Cpp_Runner.useUndefinedSanitizer": false, - "C_Cpp_Runner.useLeakSanitizer": false, - "C_Cpp_Runner.showCompilationTime": false, - "C_Cpp_Runner.useLinkTimeOptimization": false, - "C_Cpp_Runner.msvcSecureNoWarnings": false -} \ No newline at end of file