I'm trying to find the intersection point between two linked lists. I already know how to solve the problem by calculating the absolute difference in length of the two lists and displacing one of the pointers.
I want to know if it is possible to solve the question by storing addresses of each of the nodes along with a visited count in the map stores how many times the node has been visited.
int findMergeNode(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) {
map<int,int>m1;
//map<int*,int>m1 ??
//map<address,int>m1??
SinglyLinkedListNode *temp = head1;
while(temp!=nullptr) {
m1[temp]++;
temp = temp->next;
}
temp = head2;
while(temp!=nullptr) {
m1[temp]++;
temp = temp->next;
}
for(auto it=m1.begin();it!=m1.end();it++) {
if(it->second > 1) {
temp = it->first;
return temp->data;
}
}
return 0;
}