JZ36-两个链表的第一个公共结点
两个链表的第一个公共结点
https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?tpId=13&tags=&title=&diffculty=0&judgeStatus=0&rp=1&tab=answerKey
class Solution {
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
if (pHead1 == null || pHead2 == null) {
return null;
}
ListNode temp1 = pHead1, temp2 = pHead2;
while (temp1 != temp2) {
if (temp1 != null) {
temp1 = temp1.next;
} else {
temp1 = pHead2; //指针归位,画图
}
if (temp2 != null) {
temp2 = temp2.next;
} else {
temp2 = pHead1; //指针归位
}
}
return temp1;
}
} 

查看9道真题和解析