-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathintersection_of_two_sorted_LL.java
More file actions
62 lines (49 loc) · 1.57 KB
/
intersection_of_two_sorted_LL.java
File metadata and controls
62 lines (49 loc) · 1.57 KB
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
package Linkedlist;
public class intersection_of_two_sorted_LL {
Node head;
static class Node {
int data;
Node next;
Node(int data)
{
this.data=data;
this.next=null;
}
}
public Node getIntersectionNode(Node headA, Node headB) {
Node d1=headA;
Node d2=headB;
while(d1!=d2)
{
if(d1==null)
{
d1=headB;
d2=d2.next;
}
else if(d2==null)
{
d2=headA;
d1=d1.next;
}
else
{
d1=d1.next;
d2=d2.next;
}
}
return d1;
}
public static void main(String[] args) {
intersection_of_two_sorted_LL ll=new intersection_of_two_sorted_LL();
ll.head = new intersection_of_two_sorted_LL.Node(4);
ll.head.next = new intersection_of_two_sorted_LL.Node(1);
ll.head.next.next = new intersection_of_two_sorted_LL.Node(8);
intersection_of_two_sorted_LL ll2=new intersection_of_two_sorted_LL();
ll2.head = new intersection_of_two_sorted_LL.Node(5);
ll2.head.next = new intersection_of_two_sorted_LL.Node(6);
ll2.head.next.next = new intersection_of_two_sorted_LL.Node(1);
ll2.head.next.next.next = ll.head.next.next;
Node t=new intersection_of_two_sorted_LL().getIntersectionNode(ll.head,ll2.head);
System.out.println(t.data);
}
}