🧡题目描述:给定一个链表,判断链表中是否有环
💗有环就返回true 没有就返回false
function hasCycle(head){
const res = [];
while(head !=null){
if(res.includes(head)){
return true
}
res.push(head);
head = head.next
}
return false
}
分析:利用数组存储节点。判断数组中是否有此节点,有就返回true,没有就将该节点加入res中
总结:
这里的Includes方法是ES7新增的方法,用于判断数组中是否有该元素。返回的是布尔值,有该元素就返回true,没有就返回false.