1
1
package easy
2
2
3
+ import HashTableTopic
4
+ import LinkedListTopic
5
+ import TwoPointersTopic
3
6
import utils.ListNode
4
7
5
8
/* *
@@ -12,20 +15,21 @@ import utils.ListNode
12
15
* Return true if there is a cycle in the linked list. Otherwise, return false.
13
16
*/
14
17
15
- class Easy141 {
18
+ class Easy141 : HashTableTopic , LinkedListTopic , TwoPointersTopic {
16
19
17
20
fun hasCycle (head : ListNode ? ): Boolean {
18
- if (head == null ) return false
19
- if (head.next == null || head.next!! .next == null ) return false
21
+ head ? : return false
22
+ head.next ? : return false
23
+ head.next?.next ? : return false
20
24
if (head == = head.next) return true
21
25
var t1 = head
22
- var t2 = head.next!! .next
26
+ var t2 = head.next? .next
23
27
while (true ) {
24
28
when {
25
29
t1 == = t2 -> return true
26
- t2!! .next != null && t2.next!! .next != null -> {
27
- t1 = t1!! .next
28
- t2 = t2.next!! .next
30
+ t2? .next != null && t2.next!! .next != null -> {
31
+ t1 = t1? .next
32
+ t2 = t2.next? .next
29
33
}
30
34
else -> return false
31
35
}
0 commit comments