You are viewing a single comment's thread. Return to all comments →
Cpp solution with O(n) complexity.
bool has_cycle(Node* head) { map<Node*, int> ptrMap; Node *currPtr = head; while(currPtr != nullptr) { if(ptrMap.find(currPtr) != ptrMap.end()) { return true; } ptrMap[currPtr]++; currPtr = currPtr->next; } return false; }
Seems like cookies are disabled on this browser, please enable them to open this website
Linked Lists: Detect a Cycle
You are viewing a single comment's thread. Return to all comments →
Cpp solution with O(n) complexity.