File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Source: https://leetcode.com/problems/linked-list-cycle/
3
+ * ํ์ด๋ฐฉ๋ฒ: Set์ ์ด์ฉํ์ฌ ๋ฐฉ๋ฌธํ ๋
ธ๋๋ฅผ ์ ์ฅํ๊ณ ์ํํ๋ฉด์ ์ค๋ณต๋ ๋
ธ๋๊ฐ ์๋์ง ํ์ธ
4
+ *
5
+ * ์๊ฐ๋ณต์ก๋: O(n)
6
+ * ๊ณต๊ฐ๋ณต์ก๋: O(n)
7
+ */
8
+
9
+ // class ListNode {
10
+ // val: number;
11
+ // next: ListNode | null;
12
+ // constructor(val?: number, next?: ListNode | null) {
13
+ // this.val = val === undefined ? 0 : val;
14
+ // this.next = next === undefined ? null : next;
15
+ // }
16
+ // }
17
+
18
+ function hasCycle ( head : ListNode | null ) : boolean {
19
+ if ( head === null ) return false ;
20
+
21
+ // ๋ฐฉ๋ฌธํ ๋
ธ๋๋ค์ ์ ์ฅํ Set
22
+ const addr = new Set < ListNode > ( ) ;
23
+
24
+ // ์ฒซ ๋
ธ๋ ์ถ๊ฐ
25
+ addr . add ( head ) ;
26
+ head = head . next ;
27
+
28
+ // ๋ฆฌ์คํธ ์ํ
29
+ while ( head !== null ) {
30
+ // ์ด๋ฏธ ๋ฐฉ๋ฌธํ ๋
ธ๋์ธ ๊ฒฝ์ฐ cycle ์กด์ฌ
31
+ if ( addr . has ( head ) ) return true ;
32
+
33
+ // ์๋ก์ด ๋
ธ๋ ์ถ๊ฐ
34
+ addr . add ( head ) ;
35
+ head = head . next ;
36
+ }
37
+
38
+ return false ;
39
+ }
You canโt perform that action at this time.
0 commit comments