File tree 2 files changed +63
-0
lines changed
2 files changed +63
-0
lines changed Original file line number Diff line number Diff line change 12
12
| 28| [ 实现 strStr()] ( https://leetcode-cn.com/problems/implement-strstr/ ) | [ JavaScript] ( ./algorithms/implement-strstr.js ) | Easy|
13
13
| 66| [ 加一] ( https://leetcode-cn.com/problems/plus-one/ ) | [ JavaScript] ( ./algorithms/plus-one.js ) | Easy|
14
14
| 136| [ 只出现一次的数字] ( https://leetcode-cn.com/problems/single-number/ ) | [ JavaScript] ( ./algorithms/single-number.js ) | Easy|
15
+ | 141| [ 环形链表] ( https://leetcode-cn.com/problems/linked-list-cycle/ ) | [ JavaScript] ( ./algorithms/linked-list-cycle.js ) | Easy|
15
16
| 189| [ 轮转数组] ( https://leetcode-cn.com/problems/rotate-array/ ) | [ JavaScript] ( ./algorithms/rotate-array.js ) | Medium|
16
17
| 206| [ 反转链表] ( https://leetcode-cn.com/problems/reverse-linked-list/ ) | [ JavaScript] ( ./algorithms/reverse-linked-list.js ) | Easy|
17
18
| 344| [ 反转字符串] ( https://leetcode-cn.com/problems/reverse-string/ ) | [ JavaScript] ( ./algorithms/reverse-string.js ) | Easy|
Original file line number Diff line number Diff line change
1
+ // 方法一:集合(Set)
2
+
3
+ /**
4
+ * Definition for singly-linked list.
5
+ * function ListNode(val) {
6
+ * this.val = val;
7
+ * this.next = null;
8
+ * }
9
+ */
10
+
11
+ /**
12
+ * @param {ListNode } head
13
+ * @return {boolean }
14
+ */
15
+ var hasCycle = function ( head ) {
16
+ let curr = head ;
17
+ let s = new Set ( ) ;
18
+
19
+ while ( curr ) {
20
+ if ( s . has ( curr ) ) {
21
+ return true ;
22
+ }
23
+ s . add ( curr ) ;
24
+ curr = curr . next ;
25
+ }
26
+
27
+ return false ;
28
+ } ;
29
+
30
+ // 方法二:快慢指针
31
+ // Reference: https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xnwzei/
32
+
33
+ /**
34
+ * Definition for singly-linked list.
35
+ * function ListNode(val) {
36
+ * this.val = val;
37
+ * this.next = null;
38
+ * }
39
+ */
40
+
41
+ /**
42
+ * @param {ListNode } head
43
+ * @return {boolean }
44
+ */
45
+ var hasCycle = function ( head ) {
46
+ // 快慢指针
47
+ let fast = head ,
48
+ slow = head ;
49
+
50
+ while ( fast && fast . next ) {
51
+ // 慢指针每次走一步
52
+ slow = slow . next ;
53
+ // 快指针每次走两步
54
+ fast = fast . next . next ;
55
+ // 如果相遇,则说明有环
56
+ if ( fast === slow ) {
57
+ return true ;
58
+ }
59
+ }
60
+
61
+ return false ;
62
+ } ;
You can’t perform that action at this time.
0 commit comments