题解 | #链表中环的入口结点#
链表中环的入口结点
http://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4
使用set保存已遍历过的节点,遇到一样的,即为入口节点
/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function EntryNodeOfLoop(pHead)
{
// write code here
const set = new Set();
while(pHead.next!=null){
if(set.has(pHead)){
return pHead;
}
console.log(pHead);
set.add(pHead);
pHead = pHead.next;
}
return null;
}
module.exports = {
EntryNodeOfLoop : EntryNodeOfLoop
};