-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path141-linked-list-cycle.js
More file actions
53 lines (46 loc) · 1.22 KB
/
Copy path141-linked-list-cycle.js
File metadata and controls
53 lines (46 loc) · 1.22 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
function ListNode(val, next) {
this.val = (val===undefined ? 0 : val)
this.next = (next===undefined ? null : next)
}
function array2list(arr, cycleIdx) {
let cycle;
let head;
let prev = null;
let tail;
for(let i = arr.length - 1; i >= 0; i--) {
head = new ListNode(arr[i], prev);
if (i === arr.length -1) {
tail = head;
}
if (i === cycleIdx) {
cycle = head;
}
prev = head;
}
if (cycle) {
tail.next = cycle;
}
return head;
}
var hasCycle = function(head) {
if (!head || !head.next) return false;
let slow = head, fast = head;
do {
slow = slow.next;
fast = fast.next?.next;
if (slow && fast && slow === fast) return true;
} while (slow && fast);
return false;
};
const data = [
{ list: [-21,10,17,8,4,26,5,35,33,-7,-16,27,-12,6,29,-12,5,9,20,14,14,2,13,-24,21,23,-21,5], pos: -1, output: false },
{ list: [3,2,0,-4], pos: 1, output: true }
]
for (let d of data) {
console.log(JSON.stringify(d));
const list = array2list(d.list, d.pos);
const result = hasCycle(list);
console.log('result = ', JSON.stringify(result));
(JSON.stringify(result) === JSON.stringify(d.output)) ? console.log('ok') : console.error('nok');
console.log('----------');
}