Pls check the code it is giving tle
hello @sneha23
u dont have to use write ur own functions for creating a list( we dont have to create simple list , in this list we may have intersection so we need to handle that as well while constructing ). use their provided code.
and just fill the function of this code and then try to submit->
#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)
{
/*Code here*/
}
/*
*
*
* 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;
}
EXACTLY,i was struck at that…but i was notable to understand the provided template code…could you pls explain that up…btwn will it be provided alwys lyk when asked in competitions??
for two list to intersect there must atleast one node which is common in both list right?
also that common node must have same address right?
if we use our normal method then we will never be able to make a intersecting node ,becuase we always create new node using new operator which will always give new/different address of each node.
now what they are doing is they are storing address of all node in map.
let say 5 is node in first list with adrress xyz.
now if while constructing list we encounter 5 then we first check in map is 5 available. if availabe then instead of creating new address we will use same address xyz and this will ensure that the constructed list are intersecting.
no ,in online competetions they dont ask such questions.
even if they ask , they will provide u with all required functions
OH OKAY THANKYOU!!

pls check now i not able to run thisup
ur code is working fine.
pls try to submit it here->
https://hack.codingblocks.com/app/contests/1945/1334/problem
OH m sorry i was just trying to run it…i didnt tried submitting it
thankyou:)
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
