Merge pull request #1012 from xiaofei-2020/ts7

添加(0142.环形链表II.md):增加typescript版本
This commit is contained in:
程序员Carl
2022-01-19 20:04:10 +08:00
committed by GitHub

View File

@ -294,7 +294,30 @@ var detectCycle = function(head) {
};
```
TypeScript
```typescript
function detectCycle(head: ListNode | null): ListNode | null {
let slowNode: ListNode | null = head,
fastNode: ListNode | null = head;
while (fastNode !== null && fastNode.next !== null) {
slowNode = (slowNode as ListNode).next;
fastNode = fastNode.next.next;
if (slowNode === fastNode) {
slowNode = head;
while (slowNode !== fastNode) {
slowNode = (slowNode as ListNode).next;
fastNode = (fastNode as ListNode).next;
}
return slowNode;
}
}
return null;
};
```
Swift:
```swift
class Solution {
func detectCycle(_ head: ListNode?) -> ListNode? {