leetcode【 142 环形链表Ⅱ】

在这里插入图片描述

struct hashTable {
    
    
    struct ListNode* key;
    UT_hash_handle hh;
};

struct hashTable* hashtable;

struct hashTable* find(struct ListNode* ikey) {
    
    
    struct hashTable* tmp;
    HASH_FIND_PTR(hashtable, &ikey, tmp);
    return tmp;
}

void insert(struct ListNode* ikey) {
    
    
    struct hashTable* tmp = malloc(sizeof(struct hashTable));
    tmp->key = ikey;
    HASH_ADD_PTR(hashtable, key, tmp);
}

struct ListNode* detectCycle(struct ListNode* head) {
    
    
    hashtable = NULL;
    while (head != NULL) {
    
    
        if (find(head) != NULL) {
    
    
            return head;
        }
        insert(head);
        head = head->next;
    }
    return false;
}

猜你喜欢

转载自blog.csdn.net/qq_45657288/article/details/109063010