-
-
Notifications
You must be signed in to change notification settings - Fork 245
[youngduckl] WEEK 09 solutions #1910
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,44 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val) { | ||
* this.val = val; | ||
* this.next = null; | ||
* } | ||
*/ | ||
|
||
/** | ||
* @param {ListNode} head | ||
* @return {boolean} | ||
*/ | ||
var hasCycle = function (head) { | ||
// 순환탐지 기법 | ||
// 1. 플로이드 토끼와 거북이 (포인터2개) | ||
// 2. 집합을 통한 중복검사 | ||
|
||
////////풀이/////// | ||
|
||
// 노드 0개 1개면 순환불가 | ||
|
||
if (!head || !head.next) { | ||
return false; | ||
} | ||
|
||
// 토끼, 거북이 설정 | ||
let slow = head; | ||
let fast = head.next; | ||
|
||
while (slow !== fast) { | ||
// 두칸전진시 노드 존재 체크 | ||
if (!fast || !fast.next) { | ||
return false; | ||
} | ||
|
||
slow = slow.next; | ||
fast = fast.next.next; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
// 시간복잡도 O(n) -> 최대 리스트의 노드 수 만큼 while문이 반복되므로 | ||
// 공간복잡도 O(1) -> 토끼, 거북이 포인터 2개만 사용하므로 |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
달리기시합이다 보니까(??)ㅋㅋ 토끼와 거북이가 출발선을 갖게 하구
각기 한칸, 두칸씩 전진하면서 토끼가 거북이를 따라 잡으면 -> 싸이클이 있는 것
토끼가 끝에 도달하면 -> 싸이클이 없는 것
으로 짜는 방안도 좋은 것 같습니다.