-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode160.java
More file actions
94 lines (87 loc) · 3.27 KB
/
LeetCode160.java
File metadata and controls
94 lines (87 loc) · 3.27 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
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
import util.ListNode;
import util.PrintUtil;
public class LeetCode160 {
public static void main(String[] args) {
ListNode headA;
ListNode headB;
ListNode headC;
// 输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2,
// skipB = 3
// 输出:Intersected at '8'
headA = ListNode.buildLinkedList(new Integer[] { 4, 1 });
headB = ListNode.buildLinkedList(new Integer[] { 5, 6, 1 });
headC = ListNode.buildLinkedList(new Integer[] { 8, 4, 5 });
ListNode.concat(headA, headC);
ListNode.concat(headB, headC);
ListNode.printNode(new Solution160().getIntersectionNode(headA, headB));
PrintUtil.printDivider();
// 输入:intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB =
// 1
// 输出:Intersected at '2'
headA = ListNode.buildLinkedList(new Integer[] { 1, 9, 1 });
headB = ListNode.buildLinkedList(new Integer[] { 3 });
headC = ListNode.buildLinkedList(new Integer[] { 2, 4 });
ListNode.concat(headA, headC);
ListNode.concat(headB, headC);
ListNode.printNode(new Solution160().getIntersectionNode(headA, headB));
PrintUtil.printDivider();
// 输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
// 输出:null
headA = ListNode.buildLinkedList(new Integer[] { 2, 6, 4 });
headB = ListNode.buildLinkedList(new Integer[] { 1, 5 });
headC = ListNode.buildLinkedList(new Integer[] {});
ListNode.concat(headA, headC);
ListNode.concat(headB, headC);
ListNode.printNode(new Solution160().getIntersectionNode(headA, headB));
PrintUtil.printDivider();
// 输入:intersectVal = 51, listA = [2,6,4,51], listB = [1,5,51], skipA = 3, skipB
// = 51
// 输出:51
headA = ListNode.buildLinkedList(new Integer[] { 2, 6, 4 });
headB = ListNode.buildLinkedList(new Integer[] { 1, 5 });
headC = ListNode.buildLinkedList(new Integer[] { 51 });
ListNode.concat(headA, headC);
ListNode.concat(headB, headC);
ListNode.printNode(new Solution160().getIntersectionNode(headA, headB));
PrintUtil.printDivider();
}
}
class Solution160 {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
int len1 = countLen(headA);
int len2 = countLen(headB);
// 缩短长的链
if (len1 < len2) {
// headB多遍历几个点
int k = len2 - len1;
while (k > 0) {
headB = headB.next;
k--;
}
} else if (len1 > len2) {
// headA多遍历几个点
int k = len1 - len2;
while (k > 0) {
headA = headA.next;
k--;
}
}
// 搜寻相同的点
while (headA != null) {
if (headA == headB) {
return headA;
}
headA = headA.next;
headB = headB.next;
}
return null;
}
public int countLen(ListNode head) {
int count = 0;
while (head != null) {
count++;
head = head.next;
}
return count;
}
}