剑指Offer(Java版)第四十一题:输入两个链表,找出它们的第一个公共结点。
/*
输入两个链表,找出它们的第一个公共结点。
 */
import java.util.*;
public class Class41 {
	
	 public class ListNode {
	        int val;
	        ListNode next = null;
	        ListNode(int val) {
	            this.val = val;
	        }
	 }
	 
	 public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2){
		 if(pHead1 == null || pHead2 == null){
			 return null;
		 }
		 ListNode index1 = pHead1;
		 ListNode index2 = pHead2;
		 HashSet<ListNode> set = new HashSet<ListNode>();
		 while(index1 != null){
			 set.add(index1);
			 index1 = index1.next;
		 }
		 while(index2 != null){
			 if(set.contains(index2)){
				 return index2;
			 }
			 index2 = index2.next;
		 }
		 return null;
	 }
	public static void main(String[] args) {
		// TODO Auto-generated method stub
}
}
                    
                
                
            
        
浙公网安备 33010602011771号