-
-
Notifications
You must be signed in to change notification settings - Fork 195
[river20s] WEEK 09 solutions #1533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode: | ||
# def __init__(self, x): | ||
# self.val = x | ||
# self.next = None | ||
|
||
class Solution: | ||
def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
# 플로이드 토끼와 거북이 알고리즘 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 주석 설명이 기가막히네요; |
||
# 느린 포인터와 빠른 포인터, 사이클이 있다면 | ||
# 느린 포인터를 빠른 포인터가 따라 잡아 | ||
# 언젠가 같은 노드에서 만나게 될 것 | ||
# TC: O(N) | ||
# SC: O(1) | ||
# 리스트가 비어 있거나 노드가 하나뿐이면 사이클 X | ||
if not head or not head.next: | ||
return False | ||
|
||
slow = head # 느린 포인터 | ||
fast = head # 빠른 포인터 | ||
|
||
while fast is not None and fast.next is not None: # fast와 fast.next 모두 유효해야 fast.next.next 접근 가능 | ||
slow = slow.next # 느린 포인터 한 칸 이동 | ||
fast = fast.next.next # 빠른 포인터 한 칸 이동 | ||
|
||
if slow == fast: # 두 포인터가 만난다면 | ||
return True # 사이클 존재 | ||
|
||
# 루프가 끝났다면 포인터가 리스트 끝에 도달한 것이므로 사이클 X | ||
return False |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
와 주석보면서 알고리즘을 읽어보니 정말 술술 읽히네요..! 따로 검색해서 어떤 알고리즘인지 안찾아봐도 될 것 같습니다.
좋은습관 배워 갑니다! 감사합니다