mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加(0142.环形链表II.md):增加typescript版本
This commit is contained in:
@ -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:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
class Solution {
|
class Solution {
|
||||||
func detectCycle(_ head: ListNode?) -> ListNode? {
|
func detectCycle(_ head: ListNode?) -> ListNode? {
|
||||||
|
Reference in New Issue
Block a user