-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_intersection_ll.cpp
More file actions
122 lines (77 loc) · 1.77 KB
/
find_intersection_ll.cpp
File metadata and controls
122 lines (77 loc) · 1.77 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
#include "essentials.cpp"
struct Node{
int data;
struct Node*next;
};
int get_count(struct Node*head){
if(head==NULL){
return -1;
}
int count=0;
struct Node*temp=head;
while(temp!=NULL){
count++;
temp=temp->next;
}
return count;
}
void getIntersectionNode(struct Node*head1, struct Node*head2,int d){
if(head1==NULL && head2==NULL){
return;
}
else if(head1==NULL || head2==NULL){
return ;
}
struct Node*temp3=head1;
for(int i=0;i<d;i++){
temp3=temp3->next;
}
struct Node*temp4=head2;
cout<<temp3->data<<" "<<temp4->data<<endl;
while(temp3 && temp4){
if(temp3->data==temp4->data){
cout<<"intetrsection point is"<<" "<<temp3->data<<endl;
return;
}
else{
temp3=temp3->next;
temp4=temp4->next;
}
}
cout<<"no intersection point"<<endl;
return;
}
int main()
{
struct Node* newNode;
struct Node* head1 =
(struct Node*) malloc(sizeof(struct Node));
head1->data = 10;
struct Node* head2 =
(struct Node*) malloc(sizeof(struct Node));
head2->data = 3;
newNode = (struct Node*) malloc (sizeof(struct Node));
newNode->data = 6;
head2->next = newNode;
newNode = (struct Node*) malloc (sizeof(struct Node));
newNode->data = 9;
head2->next->next = newNode;
newNode = (struct Node*) malloc (sizeof(struct Node));
newNode->data = 15;
head1->next = newNode;
head2->next->next->next = newNode;
newNode = (struct Node*) malloc (sizeof(struct Node));
newNode->data = 30;
head1->next->next= newNode;
head1->next->next->next = NULL;
int count1=get_count(head1);
int count2=get_count(head2);
int d=abs(count1-count2);
if(count1>count2)
{
getIntersectionNode(head1,head2,d);}
else{
getIntersectionNode(head2,head1,d);
}
return 1;
}