-
-
Notifications
You must be signed in to change notification settings - Fork 610
/
Copy pathIntersectionofTwoLinkedLists.java
76 lines (65 loc) · 2.61 KB
/
IntersectionofTwoLinkedLists.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
package problems.easy;
import problems.utils.ListNode;
/**
* Created by sherxon on 2016-12-28.
*/
public class IntersectionofTwoLinkedLists {
/**
* Case: Find intersection node of two linked lists.
* This can be recursively and iteratively. Iterative solution: find size of both lists and move pointer
* of longer lists until it reaches the node whose length is the same as shorter list.
* Then move both pointers forward one at a time until they reach the same value. if found return it
* else there is no intersection point. Time Complexity is O(N)
* */
public ListNode getIntersectionNode(ListNode x, ListNode y) {
ListNode xHelper = x;
ListNode yHelper = y;
int sizeX = 0;
int sizeY = 0;
// get the size of each list
while (xHelper != null || yHelper != null) {
if (xHelper != null) {
xHelper = xHelper.next;
sizeX++;
}
if (yHelper != null) {
yHelper = yHelper.next;
sizeY++;
}
}
xHelper = x;
yHelper = y;
if (sizeX > sizeY) {
for (int i = 0; i < sizeX - sizeY; i++) xHelper = xHelper.next;
} else if (sizeX < sizeY) {
for (int i = 0; i < sizeY - sizeX; i++) yHelper = yHelper.next;
}
// find the intersection node
while (yHelper != null && xHelper != null) {
if (yHelper == xHelper) return xHelper;
yHelper = yHelper.next;
xHelper = xHelper.next;
}
return null;
}
/**
* This is another iterative solution without knowing size of lists.
* We can use two iterations to do that. In the first iteration, we will reset the pointer of one
* linked list to the head of another linked list after it reaches the tail node. In the second
* iteration, we will move two pointers until they points to the same node. Our operations in first
* iteration will help us counteract the difference. So if two linked list intersects,
* the meeting point in second iteration must be the intersection point. If the two linked lists
* have no intersection at all, then the meeting pointer in second iteration must be the tail
* node of both lists, which is null
* */
public ListNode getIntersectionNode2(ListNode x, ListNode y) {
if(x==null || y==null)return null;
ListNode xhead=x;
ListNode yhead=y;
while (xhead != yhead){
xhead = xhead == null ? y : xhead.next;
yhead = yhead == null ? x : yhead.next;
}
return xhead;
}
}