Skip to content

Commit f4771b0

Browse files
committed
intersection of Y LL
1 parent 357c025 commit f4771b0

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

DSA Crack Sheet/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
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)
136136
- [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)
137+
- [Intersection Point in Y Shapped Linked Lists](https://practice.geeksforgeeks.org/problems/intersection-point-in-y-shapped-linked-lists/1 "view question") - [Cpp Solution](./solutions/Intersection%20Point%20in%20Y%20Shapped%20Linked%20Lists.cpp)
137138
- []( "view question") - [Cpp Solution](./solutions/.cpp)
138139

139140
### Binary Tree
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Intersection Point in Y Shapped Linked Lists
3+
=============================================
4+
5+
Given two singly linked lists of size N and M, write a program to get the point where two linked lists intersect each other.
6+
7+
Example 1:
8+
Input:
9+
LinkList1 = 3->6->9->common
10+
LinkList2 = 10->common
11+
common = 15->30->NULL
12+
Output: 15
13+
Explanation:
14+
Y ShapedLinked List
15+
16+
Example 2:
17+
Input:
18+
Linked List 1 = 4->1->common
19+
Linked List 2 = 5->6->1->common
20+
common = 8->4->5->NULL
21+
Output: 8
22+
Explanation:
23+
24+
4 5
25+
| |
26+
1 6
27+
\ /
28+
8 ----- 1
29+
|
30+
4
31+
|
32+
5
33+
|
34+
NULL
35+
Your Task:
36+
You don't need to read input or print anything. The task is to complete the function intersetPoint() which takes the pointer to the head of linklist1(head1) and linklist2(head2) as input parameters and returns data value of a node where two linked lists intersect. If linked list do not merge at any point, then it should return -1.
37+
Challenge : Try to solve the problem without using any extra space.
38+
39+
Expected Time Complexity: O(N+M)
40+
Expected Auxiliary Space: O(1)
41+
42+
Constraints:
43+
1 ≤ N + M ≤ 2*105
44+
-1000 ≤ value ≤ 1000
45+
*/
46+
47+
int intersectPoint(Node *head1, Node *head2)
48+
{
49+
int len1 = 0, len2 = 0;
50+
auto temp1 = head1, temp2 = head2;
51+
while (temp1)
52+
{
53+
len1++;
54+
temp1 = temp1->next;
55+
}
56+
57+
while (temp2)
58+
{
59+
len2++;
60+
temp2 = temp2->next;
61+
}
62+
63+
int k = abs(len1 - len2);
64+
temp1 = head1;
65+
temp2 = head2;
66+
67+
if (len2 > len1)
68+
swap(temp1, temp2);
69+
for (int i = 0; i < k; ++i)
70+
temp1 = temp1->next;
71+
72+
while (temp1 && temp2)
73+
{
74+
if (temp1 == temp2)
75+
return temp1->data;
76+
temp1 = temp1->next;
77+
temp2 = temp2->next;
78+
}
79+
80+
return 0;
81+
}

0 commit comments

Comments
 (0)