Skip to content

Commit 357c025

Browse files
committed
intersection
1 parent e6ed860 commit 357c025

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

DSA Crack Sheet/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
- [Move last element to front of a given Linked List](https://www.geeksforgeeks.org/move-last-element-to-front-of-a-given-linked-list/ "view topic")
134134
- [Add 1 to a number represented as linked list](https://practice.geeksforgeeks.org/problems/add-1-to-a-number-represented-as-linked-list/1# "view question") - [Cpp Solution](./solutions/Add%201%20to%20a%20number%20represented%20as%20linked%20list.cpp)
135135
- [Add two numbers represented by linked lists](https://practice.geeksforgeeks.org/problems/add-two-numbers-represented-by-linked-lists/1# "view question") - [Cpp Solution](./solutions/Add%20two%20numbers%20represented%20by%20linked%20lists.cpp)
136+
- [Intersection of two sorted Linked lists](https://practice.geeksforgeeks.org/problems/intersection-of-two-sorted-linked-lists/1# "view question") - [Cpp Solution](./solutions/Intersection%20of%20two%20sorted%20Linked%20lists.cpp)
136137
- []( "view question") - [Cpp Solution](./solutions/.cpp)
137138

138139
### Binary Tree
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Intersection of two sorted Linked lists
3+
============================================
4+
5+
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.
6+
7+
Example 1:
8+
Input:
9+
L1 = 1->2->3->4->6
10+
L2 = 2->4->6->8
11+
Output: 2 4 6
12+
Explanation: For the given first two
13+
linked list, 2, 4 and 6 are the elements
14+
in the intersection.
15+
16+
Example 2:
17+
Input:
18+
L1 = 10->20->40->50
19+
L2 = 15->40
20+
Output: 40
21+
Your Task:
22+
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.
23+
24+
Expected Time Complexity : O(n+m)
25+
Expected Auxilliary Space : O(n+m)
26+
Note: n,m are the size of the linked lists.
27+
28+
Constraints:
29+
1 <= size of linked lists <= 5000
30+
1 <= Data in linked list nodes <= 1000
31+
*/
32+
33+
Node *findIntersection(Node *head1, Node *head2)
34+
{
35+
auto curr1 = head1, curr2 = head2;
36+
Node *newHead = new Node(-1);
37+
auto temp = newHead;
38+
39+
while (curr1 && curr2)
40+
{
41+
if (curr1->data == curr2->data)
42+
{
43+
Node *newNode = new Node(curr1->data);
44+
temp->next = newNode;
45+
temp = temp->next;
46+
curr1 = curr1->next;
47+
curr2 = curr2->next;
48+
}
49+
else if (curr1->data < curr2->data)
50+
curr1 = curr1->next;
51+
else
52+
curr2 = curr2->next;
53+
}
54+
55+
return newHead->next;
56+
}

0 commit comments

Comments
 (0)