Skip to content

Commit c99eade

Browse files
committed
solve problem
1 parent 189f641 commit c99eade

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

linked-list-cycle/delight010.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
public class ListNode {
2+
public var val: Int
3+
public var next: ListNode?
4+
public init(_ val: Int) {
5+
self.val = val
6+
self.next = nil
7+
}
8+
}
9+
10+
11+
class Solution {
12+
// Time O(n)
13+
// Space O(1)
14+
func hasCycle(_ head: ListNode?) -> Bool {
15+
var slow: ListNode? = head
16+
var fast: ListNode? = head
17+
18+
while fast != nil && fast?.next != nil {
19+
slow = slow?.next
20+
fast = fast?.next?.next
21+
22+
if slow === fast {
23+
return true
24+
}
25+
}
26+
return false
27+
}
28+
}
29+

0 commit comments

Comments
 (0)