Created
May 16, 2022 12:29
-
-
Save mdpabel/a2830aecd460c2319bff849950b00a1b to your computer and use it in GitHub Desktop.
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
| class Solution: | |
| def hasCycle(self, head: Optional[ListNode]) -> bool: | |
| seen = set() | |
| node = head | |
| while node: | |
| if node in seen: | |
| return True | |
| else: | |
| seen.add(node) | |
| node = node.next | |
| return False |
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
| class Solution: | |
| def hasCycle(self, head: Optional[ListNode]) -> bool: | |
| if not(head and head.next): | |
| return False | |
| slow , fast = head , head.next | |
| while fast and fast.next: | |
| if slow == fast: | |
| return True | |
| slow = slow.next | |
| fast = fast.next.next | |
| return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment