Skip to content

Commit

Permalink
Add scala solution for 141-Linked-List-Cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisKheng committed Sep 17, 2022
1 parent fd08e46 commit b34ea08
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions scala/141-Linked-List-Cycle.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Definition for singly-linked list.
* class ListNode(var _x: Int = 0) {
* var next: ListNode = null
* var x: Int = _x
* }
*/

object Solution {
def hasCycle(head: ListNode): Boolean = {
var (slowPtr, fastPtr) = (head, head)

while (fastPtr != null && fastPtr.next != null) {
slowPtr = slowPtr.next
fastPtr = fastPtr.next.next

if (slowPtr eq fastPtr) {
return true
}
}

return false
}
}

0 comments on commit b34ea08

Please sign in to comment.