判断给定的链表中是否有环
扩展:
你能给出空间复杂度O(1)的解法么?
/*
* function ListNode(x){
* this.val = x;
* this.next = null;
* }
*/
/**
*
* @param head ListNode类
* @return bool布尔型
*/
function hasCycle( head ) {
// write code here
var slow = head
var fast = head
while(fast&&fast.next){
fast = fast.next.next
slow = slow.next
if(fast == slow){
return true
}
}
return false
}
module.exports = {
hasCycle : hasCycle
};
京公网安备 11010502036488号