-
Notifications
You must be signed in to change notification settings - Fork 0
/
p173.java
100 lines (82 loc) · 2.04 KB
/
p173.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
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
/*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
Explanation:
Y ShapedLinked List
Example 2:
Input:
Linked List 1 = 4->1->common
Linked List 2 = 5->6->1->common
common = 8->4->5->NULL
Output: 8
Explanation:
4 5
| |
1 6
\ /
8 ----- 1
|
4
|
5
|
NULL
Your Task:
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.
Challenge : Try to solve the problem without using any extra space.
Expected Time Complexity: O(N+M)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N + M ≤ 2*105
-1000 ≤ value ≤ 1000*/
/* Node of a linked list
class Node {
int data;
Node next;
Node(int d) { data = d; next = null; }
}
Linked List class
class LinkedList
{
Node head; // head of list
}*/
class Intersect{
int intersectPoint(Node head1, Node head2){
int c1=count(head1);
int c2=count(head2);
int diff=Math.abs(c1-c2);
Node curr1=head1;
Node curr2=head2;
if(c1>c2){
for(int i=0;i<diff;i++){
curr1=curr1.next;
}
}
else if(c2>c1){
for(int i=0;i<diff;i++){
curr2=curr2.next;
}
}
while(curr1!=null && curr2!=null){
if(curr1==curr2){
return curr1.data;
}
curr1=curr1.next;
curr2=curr2.next;
}
return -1;
}
public static int count(Node head){
Node curr=head;
int c=0;
while(curr!=null){
c++;
curr=curr.next;
}
return c;
}
}