We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d6a2539 commit d3b8617Copy full SHA for d3b8617
โlinked-list-cycle/jinvicky.javaโ
@@ -0,0 +1,18 @@
1
+import java.util.HashSet;
2
+import java.util.Set;
3
+
4
+public class Solution {
5
+ /**
6
+ * ๋ฌธ์ ์์ ๋งํ๋ pos๋ ์ค๋ช ์ ๋๋ ์ฌ์ดํด ๋ฐ์ ์์น์ด์ง ์ฝ๋ ์์์ ์ฌ์ฉํ์ง ์๋๋ค.
7
+ */
8
+ public boolean hasCycle(ListNode head) {
9
+ Set<Integer> set = new HashSet<>();
10
+ while(head != null) {
11
+ // set์์ ์ด๋ฏธ ์กด์ฌํ๋ ์ซ์๊ฐ ์์ผ๋ฉด ๋ฐ๋ก return true;
12
+ // set.add() ๋ฉ์๋๋ ์ถ๊ฐ ์ฑ๊ณต ์ true, ์ด๋ฏธ ์กด์ฌํ๋ ์ซ์๋ฉด ์ถ๊ฐํ์ง ๋ชปํ๊ณ false๋ฅผ ๋ฐํํ๋ค.
13
+ if(!set.add(head.val)) return true;
14
+ head = head.next;
15
+ }
16
+ return false;
17
18
+}
0 commit comments