File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change
1
+ package leetcode_study
2
+
3
+ /*
4
+ * ๋งํฌ๋ ๋ฆฌ์คํธ์์ ์ํ์ด ๋ฐ์ํ๋์ง ์ฒดํฌํ๋ ๋ฌธ์
5
+ * Node `val` ๊ฐ์ ์ฃผ์ด์ง ๋ฒ์ (-10,000 <= `val` <= 10,000) ๋ณด๋ค ํฐ ์ ์๋ก ๋ณ๊ฒฝํด cycle ํ๋ณ ์๋
6
+ * ์๊ฐ ๋ณต์ก๋: O(n)
7
+ * -> linked list node ๊ฐ์๋งํผ ์งํ
8
+ * ๊ณต๊ฐ ๋ณต์ก๋: O(1)
9
+ * -> ์ฃผ์ด์ง node๋ฅผ ๊ฐ๋ฆฌํค๋ currentNode ์ด์ธ์ ์ถ๊ฐ๋๋ ์์
10
+ * */
11
+ fun hasCycle (head : ListNode ? ): Boolean {
12
+ var currentNode = head
13
+
14
+ while (currentNode?.next != null ) {
15
+ if (currentNode.`val ` == 10001 ) return true // ์ด๋ฏธ ๋ฐฉ๋ฌธํ ๋
ธ๋์ด๋ฉด ์ฌ์ดํด ์กด์ฌ
16
+ currentNode.`val ` = 10001 // ๋ฐฉ๋ฌธํ ๋
ธ๋ ํ์
17
+ currentNode = currentNode.next // ๋ค์ ๋
ธ๋๋ก ์ด๋
18
+ }
19
+
20
+ return false // `null`์ ๋ง๋ฌ๋ค๋ฉด ์ฌ์ดํด ์์
21
+ }
You canโt perform that action at this time.
0 commit comments