Added solution for problem 0142 linked list cycle 2 in python#5935
Open
Sanskar-Dwivedi wants to merge 1 commit into
Open
Added solution for problem 0142 linked list cycle 2 in python#5935Sanskar-Dwivedi wants to merge 1 commit into
Sanskar-Dwivedi wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a Python solution for LeetCode 0142 – Linked List Cycle II, implementing Floyd’s cycle detection to return the cycle entry node (or None).
Changes:
- Introduces
Solution.detectCycleusing fast/slow pointers and entry-point detection. - Adds a new Python solution file for problem 0142.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -0,0 +1,12 @@ | |||
| class Solution(object): | |||
Comment on lines
+1
to
+12
| class Solution(object): | ||
| def detectCycle(self, head): | ||
| slow=fast=head | ||
| while fast and fast.next: | ||
| slow,fast=slow.next,fast.next.next | ||
| if slow==fast: | ||
| break | ||
| else: | ||
| return None | ||
| while head!=slow: | ||
| head,slow =head.next,slow.next | ||
| return head No newline at end of file |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
0142-linked-list-cycle-ii.pyNote
Add Python solution for LeetCode 0142 Linked List Cycle II
Adds 0142-linked-list-cycle-II.py with a
Solution.detectCyclemethod using Floyd's cycle detection algorithm. The method uses slow/fast pointers to find a meeting point, then resets one pointer to head and advances both one step at a time to find the cycle entry node, returningNoneif no cycle exists.Macroscope summarized be4e1c3.