We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 32d9ab0 commit d39b804Copy full SHA for d39b804
142-linked-list-cycle-ii.js
@@ -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
24
+ start = start.next
25
+ }
26
+ return start
27
28
29
+ return null
30
+};
0 commit comments