Skip to content

Commit 9688594

Browse files
committed
feat: Add solution for LeetCode problem 141
1 parent 18697e0 commit 9688594

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

linked-list-cycle/WhiteHyun.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// 141. Linked List Cycle.swift
3+
// https://leetcode.com/problems/linked-list-cycle/description/
4+
// Algorithm
5+
//
6+
// Created by 홍승현 on 2024/05/04.
7+
//
8+
9+
import Foundation
10+
11+
final class LeetCode141 {
12+
func hasCycle(_ head: ListNode?) -> Bool {
13+
guard head != nil, head?.next != nil
14+
else {
15+
return false
16+
}
17+
18+
var tortoise = head
19+
var hare = head?.next
20+
21+
while hare != nil, hare?.next != nil {
22+
if tortoise === hare { return true }
23+
tortoise = tortoise?.next
24+
hare = hare?.next?.next
25+
}
26+
27+
return false
28+
}
29+
}

0 commit comments

Comments
 (0)