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 head1=pHead1,head2=pHead2;
while(head1!=head2)
{if(head1!=null)
{
head1=head1.next;
}
else{
//将第二个链表接到第一个链表后面
head1=pHead2;//pHead2指向的是4
}
if(head2!=null)
{
head2=head2.next;
}
else{
//将第一个链表接到第二个后面
head2=pHead1;//pHead1指向的是1
}
}
//当两个链表head1=head2时,说明都指向了同一个
//返回head1或head2都可以
return head1;
}
}