-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersection_of_two_linked_list.cpp
More file actions
150 lines (136 loc) · 2.72 KB
/
Copy pathIntersection_of_two_linked_list.cpp
File metadata and controls
150 lines (136 loc) · 2.72 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/****O(m+n) Timme Compelxity and O(m) space Complexity
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
unordered_map<ListNode*, int> m;
while(headA != NULL){
m[headA]++;
headA = headA -> next;
}
while(headB != NULL){
if(m[headB] > 0){
return headB;
}
headB = headB -> next;
}
return NULL;
}
};
****/
/*O(m+n) Time complexity O(1) space complxity*/
#include<bits/stdc++.h>
using namespace std;
class Node{
public:
int data;
Node* next;
};
void printlist(Node* head){
Node* curr=head;
while(curr!=nullptr){
cout<<curr->data<<" ";
curr=curr->next;
}
cout<<endl;
}
void point(Node* head1 , Node* head2){
int c1=0,c2=0;
Node* curr1=head1;
Node* curr2=head2;
while(curr1!=nullptr){
curr1=curr1->next;
c1+=1;
}
while(curr2!=nullptr){
curr2=curr2->next;
c2+=1;
}
int m=max(c1,c2);
int mi=min(c1,c2);
for(int i=0;i<(m-mi);i++){
head1=head1->next;
}
while(head1!=nullptr){
if(head1->data==head2->data){
cout<<head1->data<<endl;
return;
}
else{
head1=head1->next;
head2=head2->next;
}
}
cout<<"No Intersection"<<endl;
return;
}
void poi(Node* head1 , Node* head2){
unordered_map<int, bool> m;
Node* p=head1;
while(p!=nullptr){
m[p->data]=true;
p=p->next;
}
p=head2;
while(p!=nullptr){
if(m[p->data]==true){
cout<<p->data<<endl;
return;
}
else{
p=p->next;
}
}
cout<<"No intersection point"<<endl;
return;
}
void check(int t){
Node* head1=nullptr;
Node* head2=nullptr;
Node* tail1=nullptr;
Node* tail2=nullptr;
int a,b;
cout<<"A= ";
cin>>a;
for(int i=0;i<a;i++){
int x;
cin>>x;
Node* newnode=new Node{x,nullptr};
if(head1==nullptr){
head1=newnode;
tail1=newnode;
}
else{
tail1->next=newnode;
tail1=newnode;
}
}
cout<<"B= ";
cin>>b;
for(int j=0;j<b;j++){
int y;
cin>>y;
Node* n2node = new Node{y,nullptr};
if(head2==nullptr){
head2=n2node;
tail2=n2node;
}
else{
tail2->next=n2node;
tail2=n2node;
}
}
cout<<"1st list = ";
printlist(head1);
cout<<"2nd list = ";
printlist(head2);
cout<<"Point of intersection = ";
point(head1,head2);
cout<<"Point of intersection using O(m+n)";
poi(head1 , head2);
}
int main(){
int t;
cin>>t;
check(t);
return 0;
}