forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT52.java
More file actions
54 lines (44 loc) · 1.34 KB
/
Copy pathT52.java
File metadata and controls
54 lines (44 loc) · 1.34 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
package books;
import java.util.HashMap;
/**
* @program JavaBooks
* @description: 两个链表的第一个公共节点
* @author: mf
* @create: 2019/10/05 13:11
*/
/*
输入两个链表,找出它们的第一个公共节点。
*/
public class T52 {
public static void main(String[] args) {
ListNode listNode = new ListNode(1);
ListNode listNode1 = new ListNode(2);
ListNode listNode2 = new ListNode(3);
ListNode listNode3 = new ListNode(4);
ListNode listNode4 = new ListNode(5);
listNode.next = listNode2;
listNode2.next = listNode3;
listNode3.next = listNode4;
listNode1.next = listNode3;
ListNode commonNode = findFirstCommonNode(listNode, listNode1);
System.out.println(commonNode.value);
}
// 哈希
private static 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;
}
}