kt: add «141. Linked List Cycle»

URL:	https://leetcode.com/problems/linked-list-cycle/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2025-02-08 23:10:33 +01:00
parent adacef4da5
commit d7e8cc6f46
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

16
kt/linked-list-cycle.kt Normal file
View file

@ -0,0 +1,16 @@
class Solution {
fun hasCycle(head: ListNode?): Boolean {
var (x, y) = head to head
while (y != null && y!!.next != null) {
x = x!!.next
y = y!!.next!!.next
if (x == y) {
return true
}
}
return false
}
}