package demo;
import java.util.HashMap;
/*
* 题目描述
输入两个链表,找出它们的第一个公共结点。
*/
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Offer_22 {
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
ListNode current1 = pHead1;
ListNode current2 = pHead2;
HashMap<ListNode, Integer> hashMap=new HashMap<ListNode,Integer>();
while(current1 != null) {
hashMap.put(current1, null);
current1=current1.next;
}
while(current1 != null) {
if(hashMap.containsKey(current2))
return current2;
current2=current2.next;
}
return null;
}
}
//class ListNode {
// int val;
// ListNode next = null;
//
// ListNode(int val) {
// this.val = val;
// }
//}
输入两个链表,找出它们的第一个公共结点
最新推荐文章于 2021-07-25 00:24:50 发布