importUIKitclassListNode{var val:Intvar next:ListNode?init( val:Int){self.val = val
self.next =nil}}classViewController:UIViewController{overridefuncviewDidLoad(){super.viewDidLoad()}funchasCycle(head1:ListNode?,head2:ListNode?)->ListNode?{var lenOne =callen(head: head1)var lenTwo =callen(head: head2)var fast = lenOne > lenTwo ? head1 : head2
var slow = lenOne < lenTwo ? head2 : head1
var n =abs(lenOne - lenTwo);while n !=0{
n = n-1
fast = fast?.next
}while fast !=nil{if fast?.val == slow?.val {return fast
}
fast = fast?.next
slow = slow?.next
}returnnil}funccallen(head:ListNode?)->Int{var len =0, node = head
while node !=nil{
len = len +1
node = node!.next;}return len
}}