-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path142.js
More file actions
35 lines (31 loc) · 801 Bytes
/
142.js
File metadata and controls
35 lines (31 loc) · 801 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var detectCycle = function(head) {
if (head === null) return null;
if (head.next === null) return null;
let oneStepNode = head.next;
let twoStepNode = head.next.next;
if (twoStepNode === null) return null;
while(oneStepNode !== twoStepNode) {
oneStepNode = oneStepNode.next;
twoStepNode = twoStepNode.next;
if (twoStepNode === null) return null;
twoStepNode = twoStepNode.next;
if (twoStepNode === null) return null;
}
oneStepNode = head;
while(oneStepNode !== twoStepNode) {
oneStepNode = oneStepNode.next;
twoStepNode = twoStepNode.next;
}
return oneStepNode;
};