-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintersection_of_2_sorted_ll.cpp
More file actions
95 lines (63 loc) · 1.28 KB
/
intersection_of_2_sorted_ll.cpp
File metadata and controls
95 lines (63 loc) · 1.28 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
#include "essentials.cpp"
void intersection_ll(struct node**head,struct node**head2,struct node**head3){
struct node*temp=*head;
struct node*temp1=*head2;
if(*head==NULL || *head2==NULL){
return;
}
while(temp && temp1){
if(temp->data<temp1->data){
if(temp->next){
temp=temp->next;
}else{
break;
}
}
else if(temp->data>temp1->data){
if(temp1->next){
temp1=temp1->next;
}
else{
break;
}
}
else{
*head3=push_atend(*head3,temp->data);
if(temp->next && temp1->next){
temp=temp->next;
temp1=temp1->next;
}
else{
break;
}
}
}
return;
}
int main(){
struct node*head=NULL;
head=push_atend(head,3);
head=push_atend(head,5);
head=push_atend(head,6);
head=push_atend(head,7);
head=push_atend(head,14);
head=push_atend(head,22);
head=push_atend(head,29);
head=push_atend(head,30);
print_single_ll(head);
cout<<"\n";
struct node*head2=NULL;
head2=push_atend(head2,3);
head2=push_atend(head2,7);
head2=push_atend(head2,10);
head2=push_atend(head2,14);
head2=push_atend(head2,29);
head2=push_atend(head2,30);
head2=push_atend(head2,1120);
print_single_ll(head2);
cout<<"\n";
struct node*head3=NULL;
intersection_ll(&head,&head2,&head3);
print_single_ll(head3);
return 1;
}