Skip to content

Added solution in cpp Determine if a given linked list contains a cycle#295

Merged
HarshitPachori merged 6 commits into
HarshitPachori:mainfrom
kavaljeetsingh-dev:main
Oct 31, 2025
Merged

Added solution in cpp Determine if a given linked list contains a cycle#295
HarshitPachori merged 6 commits into
HarshitPachori:mainfrom
kavaljeetsingh-dev:main

Conversation

@kavaljeetsingh-dev
Copy link
Copy Markdown
Contributor

@kavaljeetsingh-dev kavaljeetsingh-dev commented Oct 31, 2025

Solution in CPP of Linked List Cycle - Hacktoberfest2025
Problem Statement

LeetCode Problem: Linked List Cycle II
Determine the node where a cycle begins in a given linked list. If no cycle exists, return null.

💡 Approach
Cycle Detection (Floyd’s Cycle Detection Algorithm):
Use two pointers:
slow moves one step at a time.
fast moves two steps at a time.
If they ever meet, a cycle exists.
Finding the Start of the Cycle:
When slow and fast meet, move slow back to head.
Move both pointers one step at a time.
The node where they meet again is the start of the cycle.
If no cycle exists:
The fast pointer will reach nullptr, and we return nullptr.

🧠 Complexity Analysis
Time Complexity: O(N)
Each node is visited at most twice (once by slow, once by fast).
Space Complexity: O(1)
No extra data structures are used, only two pointers.

Example
Input:
head = [3,2,0,-4], pos = 1
Output:
Cycle detected at node with value: 2

@github-actions
Copy link
Copy Markdown

🧩 PR Checklist

Hi @kavaljeetsingh-dev, before merging please ensure:

  • Updated contributor.md
  • Added proper commit message
  • Verified build/test success ✅

@github-actions
Copy link
Copy Markdown

⚠️ @kavaljeetsingh-dev, please add your details to the contributor.md file before merging. 🙏

@github-actions
Copy link
Copy Markdown

⭐ Hey @kavaljeetsingh-dev, if you like this project, don't forget to star the repository! 🚀

Copy link
Copy Markdown
Collaborator

@ayushHardeniya ayushHardeniya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kavaljeetsingh-dev , kindly describe your approach and the complexities of the source code, in the PR description!

Copy link
Copy Markdown
Collaborator

@ayushHardeniya ayushHardeniya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, do remove the .vscode folder & files from the Pull Request, and recommit with the changes.

@ayushHardeniya ayushHardeniya added the question Further information is requested label Oct 31, 2025
@kavaljeetsingh-dev kavaljeetsingh-dev changed the title Added solution in cpp Determine if a given linked list contains a cyc… Added solution in cpp Determine if a given linked list contains a cycle Oct 31, 2025
@kavaljeetsingh-dev
Copy link
Copy Markdown
Contributor Author

changes done

@HarshitPachori HarshitPachori merged commit 5e0d5b6 into HarshitPachori:main Oct 31, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants