mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
添加142.环形链表IIjavascript版本
This commit is contained in:
@ -258,6 +258,50 @@ Go:
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
javaScript
|
||||||
|
|
||||||
|
```js
|
||||||
|
// 两种循环实现方式
|
||||||
|
/**
|
||||||
|
* @param {ListNode} head
|
||||||
|
* @return {ListNode}
|
||||||
|
*/
|
||||||
|
// 先判断是否是环形链表
|
||||||
|
var detectCycle = function(head) {
|
||||||
|
if(!head || !head.next) return null;
|
||||||
|
let slow =head.next, fast = head.next.next;
|
||||||
|
while(fast && fast.next && fast!== slow) {
|
||||||
|
slow = slow.next;
|
||||||
|
fast = fast.next.next;
|
||||||
|
}
|
||||||
|
if(!fast || !fast.next ) return null;
|
||||||
|
slow = head;
|
||||||
|
while (fast !== slow) {
|
||||||
|
slow = slow.next;
|
||||||
|
fast = fast.next;
|
||||||
|
}
|
||||||
|
return slow;
|
||||||
|
};
|
||||||
|
|
||||||
|
var detectCycle = function(head) {
|
||||||
|
if(!head || !head.next) return null;
|
||||||
|
let slow =head.next, fast = head.next.next;
|
||||||
|
while(fast && fast.next) {
|
||||||
|
slow = slow.next;
|
||||||
|
fast = fast.next.next;
|
||||||
|
if(fast == slow) {
|
||||||
|
slow = head;
|
||||||
|
while (fast !== slow) {
|
||||||
|
slow = slow.next;
|
||||||
|
fast = fast.next;
|
||||||
|
}
|
||||||
|
return slow;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
|
Reference in New Issue
Block a user