Skip to content

Commit

Permalink
linked-list-cycle.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
Shalaahuddien committed Feb 4, 2023
1 parent 61f2768 commit eb78f2e
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions 141. Linked List Cycle.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Definition for singly-linked list.
* class ListNode(var `val`: Int, var next: ListNode? = null) {
* }
*/
class Solution {
fun hasCycle(head: ListNode?): Boolean {
var slow = head
var fast = head
while (fast?.next != null) {
slow = slow?.next
fast = fast.next?.next
if (slow == fast) {
return true
}
}
return false
}
}

0 comments on commit eb78f2e

Please sign in to comment.