import java.util.*;
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
//思路重点在于自己的链表走完,转而走对方的链表(或者重新再走一次自己的链表),直到第一次相遇。
ListNode a = pHead1;
ListNode b = pHead2;
if(a == null || b == null) return null;
while(a!=b){
a = (a==null)?pHead1:a.next;
b = (b==null)?pHead2:b.next;
}
return a;
}
}

京公网安备 11010502036488号