Skip to content

Commit d39b804

Browse files
authored
Create 142-linked-list-cycle-ii.js
1 parent 32d9ab0 commit d39b804

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

142-linked-list-cycle-ii.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
9+
/**
10+
* @param {ListNode} head
11+
* @return {ListNode}
12+
*/
13+
const detectCycle = function(head) {
14+
if(head === null || head.next === null) return null
15+
let fast = head
16+
let slow = head
17+
let start = head
18+
while(fast !== null && fast.next !== null) {
19+
fast = fast.next.next
20+
slow = slow.next
21+
if(fast === slow) {
22+
while(slow !== start) {
23+
slow = slow.next
24+
start = start.next
25+
}
26+
return start
27+
}
28+
}
29+
return null
30+
};

0 commit comments

Comments
 (0)