给访问过的节点设置标志位,如果再次访问到有标志位的节点,则证明链表中有环。

/*
 * function ListNode(x){
 *   this.val = x;
 *   this.next = null;
 * }
 */

/**
 * 
 * @param head ListNode类 
 * @return bool布尔型
 */
function hasCycle( head ) {
    while(head) {
        if(head.flag) {
            return true;
        }
        head.flag = true;
        head = head.next;
    }
    return false;
}
module.exports = {
    hasCycle : hasCycle
};