-
Notifications
You must be signed in to change notification settings - Fork 5
/
IntersectionOfSortedLists.java
54 lines (48 loc) · 1.44 KB
/
IntersectionOfSortedLists.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
/*https://practice.geeksforgeeks.org/problems/intersection-of-two-sorted-linked-lists/1*/
class Sol
{
public static Node findIntersection(Node head1, Node head2)
{
Node prev1 = head1, prev2 = head2;
//move both the heads till they are equal
while (head1.data != head2.data)
{
if (head1.data < head2.data)
{
prev1 = head1;
head1 = head1.next;
}
else if (head1.data > head2.data)
{
prev2 = head2;
head2 = head2.next;
}
}
//add more pointers
Node temp1 = head1, temp2 = head2;
//move the pointers till one of them becomes null
while (temp1 != null && temp2 != null)
{
//if the values are equal then move both
if (temp1.data == temp2.data)
{
prev1 = temp1; temp1 = temp1.next;
prev2 = temp2; temp2 = temp2.next;
}
//otherwise move the one with smaller value
else if (temp1.data < temp2.data)
{
prev1.next = temp1.next;
temp1 = prev1.next;
}
else
{
prev2.next = temp2.next;
temp2 = prev2.next;
}
}
//return the list having null at temp pointer
if (temp1 == null) return head1;
return head2;
}
}