File tree Expand file tree Collapse file tree 1 file changed +68
-0
lines changed Expand file tree Collapse file tree 1 file changed +68
-0
lines changed Original file line number Diff line number Diff line change @@ -33,3 +33,71 @@ var hasCycle = function (head) {
33
33
34
34
// ์๊ฐ๋ณต์ก๋ O(n) -> ์ต๋ ๋ฆฌ์คํธ์ ๋
ธ๋ ์ ๋งํผ while๋ฌธ์ด ๋ฐ๋ณต๋๋ฏ๋ก
35
35
// ๊ณต๊ฐ๋ณต์ก๋ O(n) -> set์ ์ต๋ ๋ฆฌ์คํธ์ ๋
ธ๋ ์ ๋งํผ size๊ฐ ์ฆ๊ฐ๋๋ฏ๋ก
36
+
37
+
38
+ /**
39
+ * Definition for singly-linked list.
40
+ * function ListNode(val) {
41
+ * this.val = val;
42
+ * this.next = null;
43
+ * }
44
+ */
45
+
46
+ /**
47
+ * @param {ListNode } head
48
+ * @return {boolean }
49
+ */
50
+ var hasCycle = function ( head ) {
51
+ if ( ! head ?. next ) {
52
+ return false ;
53
+ }
54
+
55
+ let turtle = head ;
56
+ let rabbit = head ;
57
+
58
+ while ( turtle . next && rabbit . next ) {
59
+ if ( turtle === rabbit ) {
60
+ return true ;
61
+ }
62
+
63
+ turtle = turtle . next ;
64
+ rabbit = rabbit . next . next ;
65
+ }
66
+
67
+ return false ;
68
+ } ;
69
+
70
+ /**
71
+ * Definition for singly-linked list.
72
+ * function ListNode(val) {
73
+ * this.val = val;
74
+ * this.next = null;
75
+ * }
76
+ */
77
+
78
+ /**
79
+ * @param {ListNode } head
80
+ * @return {boolean }
81
+ */
82
+ var hasCycle = function ( head ) {
83
+ if ( ! head ?. next || ! head ?. next . next ) {
84
+ return false ;
85
+ }
86
+
87
+ let turtle = head ;
88
+ let rabbit = head ;
89
+
90
+ while ( turtle && rabbit ?. next ) {
91
+ turtle = turtle . next ;
92
+ rabbit = rabbit . next . next ;
93
+
94
+ if ( turtle === rabbit ) {
95
+ return true ;
96
+ }
97
+ }
98
+
99
+ return false ;
100
+ } ;
101
+
102
+ // ์๊ฐ๋ณต์ก๋ O(n) -> ์ต๋ ๋ฆฌ์คํธ์ ๋
ธ๋ ์ ๋งํผ while๋ฌธ์ด ๋ฐ๋ณต๋๋ฏ๋ก
103
+ // ๊ณต๊ฐ๋ณต์ก๋ O(1) -> ๊ณ ์ ๋ ๋ ๋ณ์ turtle, rabbit์ ์ฌ์ฉํ๊ธฐ๋๋ฌธ์ (๊ฑฐ๋ถ์ด์ ํ ๋ผ ์๊ณ ๋ฆฌ์ฆ ์ฌ์ฉ)
You canโt perform that action at this time.
0 commit comments