forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT36.java
More file actions
32 lines (29 loc) · 801 Bytes
/
T36.java
File metadata and controls
32 lines (29 loc) · 801 Bytes
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
package web; /**
* @program LeetNiu
* @description: 两个链表的第一个公共结点
* @author: mf
* @create: 2020/01/15 09:48
*/
import java.util.HashMap;
/**
* 输入两个链表,找出它们的第一个公共结点。
*/
public class T36 {
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
if (pHead1 == null || pHead2 == null) return null;
ListNode cur1 = pHead1;
ListNode cur2 = pHead2;
HashMap<ListNode, Integer> map = new HashMap<>();
while (cur1 != null) {
map.put(cur1, 1);
cur1 = cur1.next;
}
while (cur2 != null) {
if (map.containsKey(cur2)) {
return cur2;
}
cur2 = cur2.next;
}
return null;
}
}