Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8c29d9b

Browse files
author
konstantin
committed
Easy141 challenge
1 parent 6c2854b commit 8c29d9b

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

‎src/easy/141. Linked List Cycle .kt‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package easy
22

3+
import HashTableTopic
4+
import LinkedListTopic
5+
import TwoPointersTopic
36
import utils.ListNode
47

58
/**
@@ -12,20 +15,21 @@ import utils.ListNode
1215
* Return true if there is a cycle in the linked list. Otherwise, return false.
1316
*/
1417

15-
class Easy141 {
18+
class Easy141 : HashTableTopic, LinkedListTopic, TwoPointersTopic{
1619

1720
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
2024
if (head === head.next) return true
2125
var t1 = head
22-
var t2 = head.next!!.next
26+
var t2 = head.next?.next
2327
while (true) {
2428
when {
2529
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
2933
}
3034
else -> return false
3135
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /