File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
Expand file tree Collapse file tree 1 file changed +34
-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+ * @param {ListNode } head
11+ * @return {boolean }
12+ */
13+ var hasCycle = function ( head ) {
14+ // ๋ฆฌ์คํธ๊ฐ ๋น์ด ์๊ฑฐ๋ ๋
ธ๋๊ฐ ํ๋๋ฟ์ด๋ฉด ์ฌ์ดํด์ด ์์ ์ ์์
15+ if ( ! head || ! head . next ) return false ;
16+
17+ // ๋ ํฌ์ธํฐ ์ด๊ธฐํ: slow๋ ํ ์นธ์ฉ, fast๋ ๋ ์นธ์ฉ ์ด๋
18+ let slow = head ;
19+ let fast = head . next ;
20+
21+ // fast์ slow๊ฐ ๋ง๋ ๋๊น์ง ๋ฐ๋ณต
22+ while ( fast !== slow ) {
23+ // fast๊ฐ ๋์ ๋๋ฌํ๋ฉด ์ฌ์ดํด์ด ์์
24+ if ( ! fast || ! fast . next ) return false ;
25+
26+ // slow๋ ํ ์นธ ์ด๋
27+ slow = slow . next ;
28+ // fast๋ ๋ ์นธ ์ด๋
29+ fast = fast . next . next ;
30+ }
31+
32+ // fast์ slow๊ฐ ๋ง๋ฌ๋ค๋ฉด ์ฌ์ดํด์ด ์์
33+ return true ;
34+ } ;
You canโt perform that action at this time.
0 commit comments