-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintersection_of_two_LLs.cpp
111 lines (96 loc) · 2.2 KB
/
intersection_of_two_LLs.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <unordered_map>
using namespace std;
class Node{
public:
int data;
Node* next;
Node(int data = 0){
this->data = data;
this->next = NULL;
}
~Node(){
cout<<"Deleted Node: "<<this->data<<endl;
this->next = NULL;
delete next;
}
};
void print(Node* &head){
Node* temp = head;
while(temp){
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
unordered_map<Node*,bool> mp;
Node* getIntersection(Node* &list1, Node* &list2){
Node* temp = list1;
while(temp){
mp[temp] = true;
temp = temp->next;
}
temp = list2;
while(!mp[temp] && temp){
temp = temp->next;
}
return temp;
}
Node* getIntersection_02(Node* &list1, Node* &list2){
Node* a = list1;
Node* b = list2;
while(a->next && b->next){
if(a == b)
return a;
a = a->next;
b = b->next;
}
if(a->next == NULL && b->next == NULL && a != b) return NULL;
if(a->next == NULL){
// It mean B is bigger or equal
int blen = 0;
while(b->next){
blen++;
b = b->next;
}
while(blen--){
list2 = list2->next;
}
}
else{
// It mean A is bigger
int alen = 0;
while(a->next){
alen++;
a = a->next;
}
while(alen--){
list1 = list1->next;
}
}
while(list1 != list2){
list1 = list1->next;
list2 = list2->next;
}
return list1;
}
int main(){
Node* head1 = new Node(4);
Node* sec1 = new Node(1);
Node* third1 = new Node(8);
Node* forth1 = new Node(4);
Node* fifth1 = new Node(5);
head1->next = sec1;
sec1->next = third1;
third1->next = forth1;
forth1->next = fifth1;
Node* head2 = new Node(5);
Node* sec2 = new Node(6);
Node* third2 = new Node(1);
head2->next = sec2;
sec2->next = third2;
third2->next = third1;
Node* ans = getIntersection_02(head1,head2);
print(ans);
return 0;
}