-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhasCycle.js
More file actions
executable file
·69 lines (53 loc) · 1.51 KB
/
hasCycle.js
File metadata and controls
executable file
·69 lines (53 loc) · 1.51 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Detect cycle in a linked list using Floyd's Tortoise and Hare algorithm
// example of linked list node
class ListNode {
constructor(value) {
this.value = value;
this.next = null;
}
}
function hasCycle(head) {
let slow = head;
let fast = head;
// move fast pointer two steps and slow pointer one step
while (fast !== null && fast.next !== null) {
slow = slow.next;
fast = fast.next.next;
// if slow and fast meet, there is a cycle
if (slow === fast) {
return true;
}
}
return false; // no cycle
}
// time complexity: O(n)
// space complexity: O(1)
// test the function
// helper function to create a linked list from an array
function createLinkedList(arr, pos) {
let head = new ListNode(arr[0]);
let current = head;
let cycleEntry = null;
for (let i = 1; i < arr.length; i++) {
current.next = new ListNode(arr[i]);
current = current.next;
if (i === pos) {
cycleEntry = current;
}
}
// create cycle if pos is valid
if (pos !== -1) {
current.next = cycleEntry;
}
return head;
}
let list1 = createLinkedList([3, 2, 0, -4], 1);
console.log(hasCycle(list1)); // Output: true
let list2 = createLinkedList([1, 2], 0);
console.log(hasCycle(list2)); // Output: true
let list3 = createLinkedList([1], -1);
console.log(hasCycle(list3)); // Output: false
let list4 = createLinkedList([1, 2, 3, 4, 5], -1);
console.log(hasCycle(list4)); // Output: false
let list5 = createLinkedList([], -1);
console.log(hasCycle(list5)); // Output: false