-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlistintersection.cpp
121 lines (106 loc) · 2.12 KB
/
linkedlistintersection.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
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
#include<iostream>
#include<cmath>
using namespace std;
class Node
{
public:
int data;
Node* next;
};
void printList(Node *n)
{
while(n != NULL)
{
cout << n->data << "->";
n = n->next;
}
cout << "NULL";
}
void insertAtEnd(Node* &head, int val)
{
Node* n = new Node();
n->data = val;
if(head == NULL)
{
head = n;
return;
}
Node* last = head;
while(last->next != NULL)
last = last->next;
last->next = n;
}
int length(Node* &head)
{
int l=0;
Node* temp = head;
while(temp != NULL)
{
l++;
temp = temp->next;
}
return l;
}
int intersection(Node* &head1, Node* &head2)
{
int l1 = length(head1);
int l2 = length(head2);
int d = abs(l1-l2);
Node* ptr1; // bigger list
Node* ptr2; // shorter list
if(l1>l2)
{
ptr1 = head1;
ptr2 = head2;
}
else
{
ptr1 = head2;
ptr2 = head1;
}
while(d--)
{
ptr1 = ptr1->next;
if(ptr1 == NULL) // If any list turns out to be NULL
return -1;
}
while(ptr1 != NULL && ptr2 != NULL)
{
if(ptr1 == ptr2)
return ptr1->data;
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
return -1;
}
void intersect(Node* &head1, Node* &head2, int pos)
{
Node* temp1 = head1;
Node* temp2 = head2;
pos--;
while(pos--) // Navigating to point of merger
temp1 = temp1->next;
while(temp2->next != NULL) // Navigating to end of list 2
temp2 = temp2->next;
temp2->next = temp1; // Merging
}
int main()
{
Node* head1 = new Node();
Node* head2 = new Node();
head1 = NULL;
head2 = NULL;
for(int i=0; i<6; i++) // list 1
insertAtEnd(head1, i+1);
for(int i=7; i<9; i++) // list 2
insertAtEnd(head2, i+1);
intersect(head1, head2, 5);
printList(head1);
cout << endl;
printList(head2);
cout << endl;
int x = intersection(head1, head2);
cout << head1->data << " , " << head2->data << endl;
cout << x;
return 0;
}