-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntetsectionOfLinkedList.cpp
More file actions
47 lines (42 loc) · 1.01 KB
/
IntetsectionOfLinkedList.cpp
File metadata and controls
47 lines (42 loc) · 1.01 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<unordered_map>
/****************************************************************
Following is the class structure of the Node class:
class Node
{
public:
int data;
Node *next;
Node()
{
this->data = 0;
next = NULL;
}
Node(int data)
{
this->data = data;
this->next = NULL;
}
Node(int data, Node* next)
{
this->data = data;
this->next = next;
}
};
*****************************************************************/
Node* findIntersection(Node *firstHead, Node *secondHead)
{
Node* ptr1 = firstHead;
Node* ptr2 = secondHead;
unordered_map<Node*, bool> umap;
while(ptr1)
{
umap[ptr1] = 1;
ptr1 = ptr1->next;
}
while(ptr2)
{
if(umap[ptr2]) return ptr2;
ptr2 = ptr2->next;
}
return NULL;
}