File tree Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff line change 1+ class Solution {
2+ // Time O(log n)
3+ // Space O(1)
4+ func getSum( _ a: Int , _ b: Int ) -> Int {
5+ var a = a
6+ var b = b
7+
8+ while b != 0 {
9+ var sum = a ^ b
10+ var carry = ( a & b) << 1
11+ a = sum
12+ b = carry
13+ }
14+
15+ return a
16+ }
17+ }
18+
You can’t perform that action at this time.
0 commit comments