-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathproblem149.cpp
54 lines (49 loc) · 1.07 KB
/
problem149.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
Node* findIntersection(Node* head1, Node* head2)
{
// Your Code Here
Node *t1=head1,*t2=head2;
Node *nl=NULL,*nl2=NULL;
while(t1 && t2){
if(t1->data<t2->data)
t1=t1->next;
else if(t1->data == t2->data){
if(!nl){
nl = new Node(t1->data);
nl2=nl;
}
else{
nl->next = new Node(t1->data);
nl = nl->next;
};
t1 = t1->next;
t2 = t2->next;
}
else
t2=t2->next;
}
return nl2;
}
//Find intersection point
int count(Node *head){
if(!head)
return 0;
return 1+count(head->next);
}
int intersectPoint(Node* head1, Node* head2)
{
// Your Code Here
int c1 = count(head1);
int c2 = count(head2);
Node* t = c1>=c2 ? head1:head2;
Node* s = c1<c2 ? head1:head2;
int d = abs(c1-c2);
while(d--)
t = t->next;
while(t && s){
if(t==s)
return t->data;
t = t->next;
s = s->next;
}
return -1;
}