-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion160.java
More file actions
69 lines (59 loc) · 1.26 KB
/
Copy pathQuestion160.java
File metadata and controls
69 lines (59 loc) · 1.26 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
63
64
65
66
67
68
69
/*
Author: Ananthanarayanan R
Section: Algorithms
Question: 160
*/
public class Question160
{
public static ListNode getIntersectionNode(ListNode headA, ListNode headB)
{
//length of A
int lengthA = 0;
ListNode temp = headA;
while(temp!=null)
{
lengthA++;
temp = temp.next;
}
//length of B
int lengthB = 0;
temp = headB;
while(temp!=null)
{
lengthB++;
temp = temp.next;
}
ListNode pointerA=headA,pointerB = headB;
while(pointerA!=null)
{
pointerB = headB;
while(pointerB!=null)
{
if(pointerA==pointerB)
return pointerA;
pointerB = pointerB.next;
}
pointerA = pointerA.next;
}
return null;
}
public static void main(String[] args)
{
ListNode head1 = LinkedList.create(7,"unsorted");
LinkedList.display(head1);
ListNode head2 = LinkedList.create(18,"unsorted");
LinkedList.display(head2);
ListNode headA = head1,headB = head2;
/*
for(int i = 0;i<4;i++)
head1 = head1.next;
for(int i = 0;i<7;i++)
head2 = head2.next;
head1.next = head2;
LinkedList.display(head1);
LinkedList.display(head2);
*/
ListNode result = getIntersectionNode(headA,headB);
LinkedList.display(result);
}
}