-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIntersectionNote.java
More file actions
60 lines (51 loc) · 1.5 KB
/
IntersectionNote.java
File metadata and controls
60 lines (51 loc) · 1.5 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
package com.algorithm.linkedlists;
import static com.algorithm.linkedlists.PrintNode.printout;
public class IntersectionNote {
/**
* #160
* https://leetcode-cn.com/problems/intersection-of-two-linked-lists
*
* @param args
*/
public static void main(String[] args) {
ListNode headA = new ListNode(0);
headA.next = new ListNode(9);
headA.next.next = new ListNode(1);
headA.next.next.next = new ListNode(2);
headA.next.next.next.next = new ListNode(4);
ListNode headB = new ListNode(3);
headB.next = new ListNode(2);
headB.next.next = new ListNode(4);
printout(new IntersectionNote().getIntersectionNode(headA, headB));
}
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) {
return null;
}
int l1 = getNodeLen(headA), l2 = getNodeLen(headB);
if (l1 > l2) {
return helper(headA, headB, l1 - l2);
}
return helper(headB, headA, l2 - l1);
}
static ListNode helper(ListNode h1, ListNode h2, int i) {
while (i > 0) {
h1 = h1.next;
i--;
}
while (h1 != h2) {
h1 = h1.next;
h2 = h2.next;
}
return h1;
}
static int getNodeLen(ListNode node) {
ListNode p = node;
int len = 0;
while (p != null) {
len++;
p = p.next;
}
return len;
}
}