//方法一:運用HashMap的特性 import java.util.HashMap; public class Solution { public ListNode findFristCommonNode(ListNode pHead1,ListNode pHead2){ ListNode current1 = pHead1; ListNode current2 = pHead2; Map<ListNode,Integer> haspMap = new HashMap<ListNode,Integer>(); while(current1 != null){ hashMap.put(currnet1,null); current1 = current1.next; } while(current2 != null){ if(haspMap.comtainsKey(current2)){ return current2; } } return null; } } //方法2 長鏈表先走 len1 - len2 public ListNode findFirstCommonNode(ListNode pHead1,ListNode pHead2){ ListNode current1 = pHead1; ListNode current2 = pHead2; if(pHead1 == null || pHead2 == null){ return null; } int len1 = getLength(pHead1); int len2 = getLength(pHead2); if(len1 > len2){ int len = len1 - len2; while(len>0){ current1 = current1.next; len--; } }else if(len1 < len2){ int len = len2 - len1; while(len>0){ currnet2 = current2.next; len--; } } while(current1 != null && current2 != null){ if(current1 == current2){ return current1; } current1 = currnet1.next; current2 = current2.next; } return null; } public int getLength(ListNode pHead){ ListNode current = pHead; int count=0; while(current != null){ count++; current = current.next; } return count; }