Skip to content

Commit

Permalink
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
Browse files Browse the repository at this point in the history
  • Loading branch information
youngyangyang04 committed Aug 25, 2021
2 parents 45aa719 + 6b729ca commit 5726a73
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions problems/0142.环形链表II.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,39 @@ var detectCycle = function(head) {
};
```

Swift:
```swift
class Solution {
func detectCycle(_ head: ListNode?) -> ListNode? {
var slow: ListNode? = head
var fast: ListNode? = head
while fast != nil && fast?.next != nil {
slow = slow?.next
fast = fast?.next?.next
if slow == fast {
// 环内相遇
var list1: ListNode? = slow
var list2: ListNode? = head
while list1 != list2 {
list1 = list1?.next
list2 = list2?.next
}
return list2
}
}
return nil
}
}
extension ListNode: Equatable {
public func hash(into hasher: inout Hasher) {
hasher.combine(val)
hasher.combine(ObjectIdentifier(self))
}
public static func == (lhs: ListNode, rhs: ListNode) -> Bool {
return lhs === rhs
}
}
```

-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
Expand Down

0 comments on commit 5726a73

Please sign in to comment.