leetcode hashtable 142 lista enlazada circular II

Directorio de artículos

LA

problema

Inserte la descripción de la imagen aquí

lograr

Ideas

Este problema se puede resolver mediante derivación matemática para acelerar el puntero lento, pero no se puede resolver si no se ha hecho. Usamos una tabla hash para directamente AC de fuerza bruta.

Resumen y reflexión

  1. El método de doble puntero no se ha realizado antes, pero ac no sale.

Código


/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };

这道题可以通过数学推导加快慢指针解答, 但是没有做过基本解不出来。 我们使用哈希表,直接暴力ac一下。



*/
class Solution {
    
    
public:
   ListNode *detectCycle(ListNode *head) {
    
    
       unordered_set<ListNode*> visitor;
       while(head){
    
    
           if(visitor.count(head)) return head;
           visitor.insert(head);
           head = head->next;
       }
       return nullptr;
   }
};

Supongo que te gusta

Origin blog.csdn.net/liupeng19970119/article/details/114044858
Recomendado
Clasificación