Linked List Cycle (Unordered map)(Non optimised)

PHOTO EMBED

Tue May 28 2024 21:55:39 GMT+0000 (Coordinated Universal Time)

Saved by @ayushg103 #c++

 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;
    }
content_copyCOPY

You might think that before writing code that how will loop end when it does not have null. ans is it will return or break;