-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlistCycle.js
69 lines (55 loc) · 1.21 KB
/
linkedlistCycle.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// 141. Linked List Cycle
// https://leetcode.com/problems/linked-list-cycle/
class LLNode {
constructor(val, next) {
this.val = val;
this.next = next;
}
}
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {boolean}
*/
// var hasCycle = function(head) {
// let fast = head;
// let slow = head;
// while(fast && fast.next) {
// fast = fast.next.next;
// slow = slow.next;
// if(fast===slow) {
// console.log(fast)
// console.log(slow)
// return true;
// }
// }
// return false;
// };
var hasCycle = function(head) {
let current = head;
while(current) {
if(current.val === 'p') {
return true
}
current.val = 'p';
current = current.next;
}
return false
};
const ll = new LLNode(30);
const node33 = new LLNode(33);
const node43 = new LLNode(43);
const node90 = new LLNode(90);
const node58 = new LLNode(91);
ll.next = node33;
node33.next = node43;
node43.next = node90;
node90.next = node58;
console.log(ll)
console.log(hasCycle(ll))