bool hasCycle(ListNode *head) { ListNode* temp = head; unordered_map<ListNode*,int> m; while(temp != NULL) { m[temp]++; if(m[temp] == 2) { return true; } temp = temp->next; } return false; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter