-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersection_Of_LL.cpp
More file actions
67 lines (67 loc) · 962 Bytes
/
Intersection_Of_LL.cpp
File metadata and controls
67 lines (67 loc) · 962 Bytes
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
//Intersection of two linked lists
#include<iostream>
using namespace std;
class node{
public:
int data;
node*next;
node(int data){
this->data=data;
}
};
void build(node*&head,int n){
int data,i=1;
while(i<=n){
cin>>data;
node*n=new node(data);
n->next=NULL;
if(head==NULL){
head=n;
}
else{
node*save=head;
while(save->next!=NULL){
save=save->next;
}
save->next=n;
}
i++;
}
}
void print(node*head){
while(head!=NULL){
cout<<head->data<<" ";
head=head->next;
}
}
int main(){
node*head=NULL;
node*head2=NULL;
int n,m;
cin>>n;
build(head,n);
cin>>m;
build(head2,m);
int d=n-m,i=1;
node*c,*c2;
if(d<0){
cout<<-1;
}
else{
c=head;
while(i<=d){
c=c->next;
i++;
}
c2=head2;
while(c2->data!=c->data&&c!=NULL){
c2=c2->next;
c=c->next;
}
if(c==NULL){
cout<<-1;
}
else;
cout<<c->data;
}
}