#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node(int d)
{
data = d;
next = NULL;
}
};
// This function gets two arguments - the head pointers of the two linked lists
// Return the Node which is the intersection point of these linked lists
// It is assured that the two lists intersect
Node intersectionOfTwoLinkedLists(Node l1, Node l2)
{
Node fast=l1;
Node slow=l2;
Node temp1=l1;
Node* temp2=l2; /Code here/
int length1=0,length2=0;
while(temp1!=NULL)
{
temp1=temp1->next;
length1++;
}
while(temp2!=NULL)
{
temp2=temp2->next;
length2++;
}
int d=length1-length2;
if(length2>length1)
{
Node* temp=fast;
fast=slow;
slow=temp;
d=length2-length1;
}
for(int i=1;i<=d;i++)
{
fast=fast->next;
}
while(fast!=NULL && slow!=NULL)
{
if(fast==slow)
return slow;
fast=fast->next;
slow=slow->next;
}
return NULL;
}
/*
*
*
- You do not need to refer or modify any code below this.
- Only modify the above function definition.
- Any modications to code below could lead to a ‘Wrong Answer’ verdict despite above code being correct.
- You do not even need to read or know about the code below.
*/
Node *buildList(unordered_map<int, Node *> &hash)
{
int x;
cin >> x;
Node *head = new Node(x);
Node *current = head;
hash[x] = head;
while (x != -1)
{
cin >> x;
if (x == -1)
break;
Node *n = new Node(x);
hash[x] = n;
current->next = n;
current = n;
}
current->next = NULL;
return head;
}
void printLinkedList(Node *head)
{
while (head != NULL)
{
cout << head->data << " ";
head = head->next;
}
cout << endl;
}
int main()
{
unordered_map<int, Node *> hash;
Node *l1 = buildList(hash);
Node *l2 = NULL;
int x;
cin >> x;
l2 = new Node(x);
Node *temp = l2;
while (x != -1)
{
cin >> x;
if (x == -1)
break;
if (hash.find(x) != hash.end())
{
temp->next = hash[x];
break;
}
Node *n = new Node(x);
temp->next = n;
temp = n;
}
cout << "L1 - ";
printLinkedList(l1);
cout << "L2 - ";
printLinkedList(l2);
Node *intersectionPoint = intersectionOfTwoLinkedLists(l1, l2);
cout << "Intersection at Node with data = " << intersectionPoint->data << endl;
return 0;
}