Skip to content

Commit c957428

Browse files
author
AkshayChavan
committed
did some changes
1 parent 90999ea commit c957428

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

Data-Structures/Linked-List/CycleDetectionII.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
/**
2-
* A LinkedList based solution for Detecting a start node of the Cycle in a list.
3-
* https://en.wikipedia.org/wiki/Cycle_detection
2+
* A LinkedList based solution for finding the starting node of the cycle in a list.
3+
* @returns the node where cycle begins in the linked list. If there is no cycle present, returns null.
4+
* @see https://en.wikipedia.org/wiki/Cycle_detection
5+
* @see https://leetcode.com/problems/linked-list-cycle-ii/
46
*/
57

68
function detectCycleNode(head) {
7-
/*
8-
Problem Statement:
9-
Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null.
10-
Link for the Problem: https://leetcode.com/problems/linked-list-cycle-ii/
11-
*/
12-
9+
1310
let length = 0
1411
let fast = head
1512
let slow = head
@@ -25,14 +22,12 @@ function detectCycleNode(head) {
2522
}
2623

2724
if (length === 0) {
28-
// If length = 0, return null
25+
// If there is no cycle, return null.
2926
return null
3027
}
3128

32-
//Point the nodes at head
33-
fast = head
34-
slow = head
35-
// Move slow pointer ahead by 'length' of cycle times
29+
fast = slow = head
30+
// Move slow pointer ahead by 'length' (integer) of cycle times
3631
while (length > 0) {
3732
slow = slow.next
3833
length--
@@ -44,7 +39,7 @@ function detectCycleNode(head) {
4439
slow = slow.next
4540
}
4641

47-
// return the meeting node
42+
// return the meeting node (fast/slow)
4843
return slow
4944
}
5045

@@ -74,4 +69,4 @@ function lengthCycle(head) {
7469
return 0
7570
}
7671

77-
export { detectCycleNode }
72+
export { detectCycleNode }

0 commit comments

Comments
 (0)