#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
int length(Node *head) {
int count = 0;
while (head != NULL) {
count++;
head = head->next;
}
return count;
}
Node *intersectionOfTwoLinkedLists(Node *l1, Node *l2)
{
int len1 = length(l1), len2 = length(l2);
if (len2 > len1){
swap(l1, l2);
swap(len1, len2);
}
// cout << len1 << len2;
// Node *ans = NULL;
int diff = len1 - len2;
int count = 0;
while(count < diff){
l1 = l1->next;
}
while(l1->next != NULL and l2->next != NULL){
l1 = l1->next;
l2 = l2->next;
if (l1 == l2){
return l1;
}
}
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;
}