forked from zfman/AlgorithmCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindFirstCommonNodeSolution.java
More file actions
74 lines (64 loc) · 1.59 KB
/
FindFirstCommonNodeSolution.java
File metadata and controls
74 lines (64 loc) · 1.59 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
package offer;
import leetcode.common.LinkedUtils;
import leetcode.common.ListNode;
/**
* 两个链表的第一个公共结点
*
* 输入两个链表,找出它们的第一个公共结点。
*
* @author 刘壮飞
* https://github.com/zfman.
* https://blog.csdn.net/lzhuangfei.
*/
public class FindFirstCommonNodeSolution {
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
int m=0,n=0;
ListNode p1=pHead1,p2=pHead2;
while (p1!=null){
p1= p1.next;
m++;
}
while (p2!=null){
p2= p2.next;
n++;
}
p1=pHead1;
p2=pHead2;
int k=0;
if(m>n){
while(k<m-n){
p1=p1.next;
k++;
}
}else{
while(k<n-m){
p2=p2.next;
k++;
}
}
while (p1!=null&&p2!=null){
if(p1==p2) return p1;
p1=p1.next;
p2=p2.next;
}
return null;
}
public static void main(String[] args){
int[] arr1={
1,2,3
};
int[] arr2={
4,5
};
int[] arr3={
7,8,9
};
ListNode node1=LinkedUtils.arrayToLinkedList(arr1);
ListNode node2=LinkedUtils.arrayToLinkedList(arr2);
ListNode node3=LinkedUtils.arrayToLinkedList(arr3);
node1.next=node3;
node2.next=node3;
ListNode r=new FindFirstCommonNodeSolution().FindFirstCommonNode(node1,node2);
LinkedUtils.print(r);
}
}