File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /**
2+ ๊ธธ์ด๊ฐ ์ต๋ 10000์ด๋ฏ๋ก 10001๊น์ง ๊ฒฝ์ฐ์ ์๋ฅผ ๋์ ํ์ธํ๋ ๋ฐฉ๋ฒ
3+ */
4+ public class Solution {
5+ public boolean hasCycle (ListNode head ) {
6+ int max = 10001 ;
7+ int idx = 0 ;
8+ boolean result = false ;
9+ while (head != null ) {
10+ idx ++;
11+ head = head .next ;
12+ if (idx > max ) {
13+ result = true ;
14+ break ;
15+ }
16+ }
17+
18+ return result ;
19+ }
20+ }
21+ /**
22+ 2๊ฐ์ ํฌ์ธํฐ๋ฅผ ์ด์ฉํ์ฌ, ๋ง๋๋์ง ํ์ธํ๋ ๋ฐฉ๋ฒ
23+ */
24+ public class Solution2 {
25+ public boolean hasCycle (ListNode head ) {
26+ ListNode first = head ;
27+ ListNode second = head ;
28+ boolean result = false ;
29+ while (second != null && second .next != null ) {
30+ first = first .next ;
31+ second = second .next .next ;
32+ if (first == second ) {
33+ result = true ;
34+ break ;
35+ }
36+ }
37+
38+ return result ;
39+ }
40+ }
You canโt perform that action at this time.
0 commit comments