leetcode 141:环形链表

题目:环形链表

  • 题目面述:
    给定一个链表,判断链表中是否有环。
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        std::set<ListNode *> node_set;
        while(head){
            if(node_set.find(head)!=node_set.end()){
                return true;
            }
            node_set.insert(head);
            head = head->next;
        }
        return false;
    }
};

猜你喜欢

转载自blog.csdn.net/gjpzl/article/details/80151269
今日推荐