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:
parent
adacef4da5
commit
d7e8cc6f46
1 changed files with 16 additions and 0 deletions
16
kt/linked-list-cycle.kt
Normal file
16
kt/linked-list-cycle.kt
Normal 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
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue