-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathLinkedListCycle
36 lines (28 loc) · 829 Bytes
/
LinkedListCycle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Source: LeetCode-141: https://leetcode.com/problems/linked-list-cycle/description/
// Solution Ref: https://www.youtube.com/watch?v=agkyC-rbgKM&ab_channel=FisherCoder
// Example head = [3,2,0,-4], pos = 1
// Output: true
/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init(_ val: Int) {
* self.val = val
* self.next = nil
* }
* }
*/
class Solution {
func hasCycle(_ head: ListNode?) -> Bool {
guard let head = head else { return false }
var fast = head.next
var slow = head
while(fast != nil && slow != nil) {
if fast === slow { return true }
fast = fast?.next?.next
slow = slow.next!
}
return false
}
}