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) {
        /*set<ListNode*> node_map;
        while(head)
        {
            if(node_map.find(head) != node_map.end())
                return true;
            else
                node_map.insert(head);
            head = head->next;
            
        }
        return false;*/


        ListNode *slow = head; 
        ListNode *fast = head;
        if(head == NULL || head->next == NULL)
            return false;
        while(slow->next && fast->next!=NULL &&fast->next->next)

while(slow->next && fast->next->next) 错误member access within null pointer of type 'struct ListNode'


        {
            slow = slow->next;
            fast = fast->next->next;
            if(slow == fast)
                return true;
            
        }
        return false;
        
        
    }
};


版权声明:本文为qq_26641149原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。