-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathIntersection Point Of Two Linked List.cpp
156 lines (132 loc) · 3.48 KB
/
Intersection Point Of Two Linked List.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* Leet Code */
/* Title - Intersection Point Of Two Linked List */
/* Created By - Akash Modak */
/* Date - 2/8/2020 */
// Given two intersecting linked lists, write a function to find its point of intersection. If the lists do not intersect , return NULL.
// Note : You are required to only write a single function. Do not modify / alter the remaining code.
// This is a stub problem so you need not worry about taking input or displaying output. Only focus on the designated function.
// Input Format
// You are given a function which accepts head pointers of two linked lists.
// Constraints
// Your function should run in linear time.
// Output Format
// Return the intersection point node.
// Sample Input
// Consider these linked lists :
// 1 -> 2 -> 3 -> null
// ↗
// 4
// (This is not the actual input that will be provided but rather a description of it)
// Sample Output
// The two linked lists 1->2->3->null and 4->2->3-> null intersect at node with data 2.
// Return the node with data = 2.
// Explanation
// The two linked lists intersect at 2. Return their intersection point.
#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*/
map<int,int> m;
Node* temp=NULL;
Node *t1 = l1;
Node *t2 = l2;
while(t1!=NULL){
m[t1->data]++;
t1=t1->next;
}
while(t2!=NULL){
m[t2->data]++;
if(m[t2->data]>1){
temp=t2;
break;
}
t2=t2->next;
}
return temp;
}
/*
*
*
* 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;
}