forked from imsushant12/GFG-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Intersection_of_two_sorted_Linked_lists.cpp
62 lines (54 loc) · 1.53 KB
/
Intersection_of_two_sorted_Linked_lists.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
/*
Problem Statement:
------------------
Given two lists sorted in increasing order, create a new list representing the intersection of the two lists.
The new list should be made with its own memory — the original lists should not be changed.
Example 1:
----------
Input:
L1 = 1->2->3->4->6
L2 = 2->4->6->8
Output: 2 4 6
Explanation: For the given first two linked list, 2, 4 and 6 are the elements in the intersection.
Example 2:
---------
Input:
L1 = 10->20->40->50
L2 = 15->40
Output: 40
Your Task: The task is to complete the function intersection() which should find the intersection of two linked list and add all the elements
in intersection to the third linked list and return the head of the third linked list.
Expected Time Complexity : O(n+m)
Expected Auxilliary Space : O(n+m)
Note: n,m are the size of the linked lists.
*/
// Link --> https://practice.geeksforgeeks.org/problems/intersection-of-two-sorted-linked-lists/1#
// Code:
Node* findIntersection(Node* head1, Node* head2)
{
Node *h = head1;
Node *head = NULL , *tail = NULL;
unordered_set <int> us;
while(h != NULL)
{
us.insert(h->data);
h = h->next;
}
h = head2;
while(h != NULL)
{
if(us.find(h->data) != us.end())
{
if(head == NULL )
head = tail = new Node(h->data);
else
{
tail->next = new Node(h->data);
tail = tail->next;
}
us.erase(h->data);
}
h = h->next;
}
return head;
}