-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathSlowFast.js
49 lines (41 loc) · 1.32 KB
/
SlowFast.js
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// SlowFast.js
// Definition for singly-linked list node
class ListNode {
constructor(value) {
this.value = value;
this.next = null;
}
}
/**
* Detects if a linked list has a cycle.
* @param {ListNode} head - The head of the linked list.
* @returns {boolean} - True if there's a cycle, false otherwise.
*/
function hasCycle(head) {
let slow = head;
let fast = head;
while (fast && fast.next) {
slow = slow.next; // Move slow pointer one step
fast = fast.next.next; // Move fast pointer two steps
if (slow === fast) {
return true; // Cycle detected
}
}
return false; // No cycle
}
/**
* Creates a linked list from an array of values.
* @param {Array<number>} values - The values to create the linked list from.
* @returns {ListNode|null} - The head of the created linked list.
*/
function createLinkedList(values) {
const dummyHead = new ListNode(0);
let current = dummyHead;
for (const value of values) {
current.next = new ListNode(value);
current = current.next;
}
return dummyHead.next; // Return the head of the created linked list
}
// Exporting the ListNode class and functions for testing
export { ListNode, hasCycle, createLinkedList }; // Ensure ListNode is exported