-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinkedListCycle.cpp
33 lines (32 loc) · 905 Bytes
/
LinkedListCycle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <unordered_map>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
//HASHMAP OR FLOYD CYCLE DETECTION: Fast pointer moves 2, slow pointer moves 1.
//IF POINTERS EQUAL, THEN CYCLE!
// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
unordered_map<ListNode*,int> visited;
ListNode* currNode = head;
visited.insert({currNode,0});
int i = 1;
if(!currNode)return NULL;
while(currNode->next){
if(visited.find(currNode->next)==visited.end()){
visited.insert({currNode->next,i});
i++;
currNode= currNode->next;
}
else return currNode->next;
}
return NULL;
}
};