/** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } *//** * @param {ListNode} head * @return {boolean} */var hasCycle = function(head) { if(!head || !head.next) return false let cur = head.next const map = new Map() while(cur) { if(map.has(cur)) { return true } map.set(cur) cur = cur.next } return false};
/** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } *//** * @param {ListNode} head * @return {boolean} */var hasCycle = function(head) { if(!head || !head.next) return false let slow = head.next let fast = head.next.next while(slow) { if(fast === null || fast.next === null) return false slow = slow.next fast = fast.next.next if(slow === fast) return true } return false};