#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)
{
int length1=0;
int length2=0;
Node *p=l1;
Node *q=l2;
while(p!=NULL)
{
length1++;
p=p->next;
}
while(q!=NULL)
{
length2++;
q=q->next;
}
int diff=0;
p=l1;
q=l2;
if(length1>length2)
{
diff=length1-length2;
for(int i=0;i<diff;i++)
p=p->next;
}
else
{
diff=length2-length1;
for(int i=0;i<diff;i++)
q=q->next;
}
while(p!=NULL && q!=NULL)
{
p=p->next;
q=q->next;
if(p==q)
return p;
}
}
/*
*
*
- 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;
}
plz check this function
Node *intersectionOfTwoLinkedLists(Node *l1, Node *l2)