题解 | #旅行牛#

旅行牛

https://www.nowcoder.com/practice/5d9c86c84737493e824593d86cf94efd

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 *	ListNode(int x) : val(x), next(nullptr) {}
 * };
 */
#include <functional>
#include <unordered_map>
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param head ListNode类 
     * @return bool布尔型
     */
    bool hasCycle(ListNode* head) {
        // write code here
        bool map[100001]={0};
        while(head)
        {   
            if(map[head->val])
            {
                return true;
            }
            else{
                map[head->val] =true;
                head = head->next;
            }
        }
        return false;
    }
};

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务