Share my C++ solution(use map to handle)


  • 0
    S
    bool hasCycle(ListNode *head) {
          map<string, int> myMap;
          while (head != NULL) {
                char addr[12] = {0};
                sprintf(addr,"%p",head);
                map<string, int>::iterator key = myMap.find(addr);
                if ( key != myMap.end() ) {
                      return true;
                } else {
                      myMap.insert(pair<string, int>(addr ,1));
                }
                head = head->next;
          }
          return false;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.