File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for singly-linked list.
3+ * function ListNode(val) {
4+ * this.val = val;
5+ * this.next = null;
6+ * }
7+ */
8+
9+ /**
10+ * ์ฐ๊ฒฐ ๋ฆฌ์คํธ์ ์ฌ์ดํด ์ฌ๋ถ๋ฅผ ๋ฐํํ๋ ํจ์
11+ * ์๊ฐ๋ณต์ก๋: O(n)
12+ * ๊ณต๊ฐ๋ณต์ก๋: O(1)
13+ * @param {ListNode } head
14+ * @return {boolean }
15+ */
16+ const hasCycle = function ( head ) {
17+ let node = head ;
18+
19+ while ( node ?. next ) {
20+ if ( node . val === null ) {
21+ return true ;
22+ }
23+ node . val = null ;
24+ node = node . next ;
25+ }
26+
27+ return false ;
28+ } ;
29+
30+ // ๊ฑฐ๋ถ์ด์ ํ ๋ผ ์๊ณ ๋ฆฌ์ฆ
31+ // ์๊ฐ๋ณต์ก๋: O(n)
32+ // ๊ณต๊ฐ๋ณต์ก๋: O(1)
33+ const hasCycle = function ( head ) {
34+ let slow = head ; // ํ ๋ฒ์ ํ ๋
ธ๋์ฉ
35+ let fast = head ; // ํ ๋ฒ์ ๋ ๋
ธ๋์ฉ
36+
37+ while ( fast ?. next ) {
38+ slow = slow . next ;
39+ fast = fast . next . next ;
40+
41+ // ํ ๋ฒ์ ๋ ๋
ธ๋์ฉ ๊ฐ๋ fast๊ฐ
42+ // ์์ ๋ณด๋ค ๋๋ฆฐ slow๋ ๊ฐ์ ๊ฒฝ์ฐ๊ฐ ์๊ธด๋ค๋ฉด
43+ // ์ฌ์ดํด์ด ์๋ค๋ ๋ป
44+ if ( slow === fast ) {
45+ return true ;
46+ }
47+ }
48+
49+ return false ;
50+ } ;
You canโt perform that action at this time.
0 commit comments