-
-
Notifications
You must be signed in to change notification settings - Fork 361
[yuseok89] WEEK 09 Solutions #2824
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
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. 조건문 부분에 같은지 비교를 하는것도 꽤 괜찮은 아이디어시네요! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # TC: O(N) | ||
| # SC: O(1) | ||
| # 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: | ||
| if not head or not head.next: | ||
| return False | ||
|
Comment on lines
+11
to
+12
Member
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. 노드가 하나뿐이고 순환이 없는 입력이라면 |
||
|
|
||
| slow, fast = head, head.next | ||
| while slow != fast: | ||
| if not fast or not fast.next: | ||
| return False | ||
|
|
||
| slow, fast = slow.next, fast.next.next | ||
|
|
||
| return True | ||
|
|
||
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.
🏷️ 알고리즘 패턴 분석
linked-list-cycle/yuseok89.py
📊 시간/공간 복잡도 분석
피드백: 빠른 포인터와 느린 포인터를 이용해 순회하므로 추가 메모리 없이 사이클 여부를 판단할 수 있다.
개선 제안: 현재 구현이 적절해 보입니다.