-
Notifications
You must be signed in to change notification settings - Fork 87
/
intersection_point_linked_list.java
58 lines (47 loc) · 1.44 KB
/
intersection_point_linked_list.java
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
// Intersection Point in Y Shaped Linked Lists
// Problem Link: https://practice.geeksforgeeks.org/problems/intersection-point-in-y-shapped-linked-lists/1?utm_source=gfg&utm_medium=article&utm_campaign=bottom_sticky_on_article
// Given two singly linked lists of size N and M, write a program to get the point where two linked lists intersect each other.
// Example 1:
// Input:
// LinkList1 = 3->6->9->common
// LinkList2 = 10->common
// common = 15->30->NULL
// Output: 15
class Intersect
{
//Function to find intersection point in Y shaped Linked Lists.
int intersectPoint(Node head1, Node head2)
{
// code here
int size1=0;
int size2=0;
Node temp1=head1;
Node temp2=head2;
while(temp1!=null){
size1++;
temp1=temp1.next;
}
while(temp2!=null){
size2++;
temp2=temp2.next;
}
int delta=Math.abs(size1-size2);
if(size1>size2){
for(int i=0;i<delta;i++){
head1=head1.next;
}
}else{
for(int i=0;i<delta;i++){
head2=head2.next;
}
}
while(head1!=head2){
head1=head1.next;
head2=head2.next;
}
if(head1!=null)
return head1.data;
else
return -1;
}
}