/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
*
* @param pHead1 ListNode类
* @param pHead2 ListNode类
* @return ListNode类
*/
struct ListNode* FindFirstCommonNode(struct ListNode* pHead1, struct ListNode* pHead2 ) {
struct ListNode* l1 =pHead1;
struct ListNode* l2 =pHead2;
while(l1!=l2){
l1=(l1==NULL)?pHead2:l1->next;
l2=(l2==NULL)?pHead1:l2->next;
}
return l1;
// write code here
}
这个代码简洁性和算法值得学习,一般思路为l1每走一个,遍历l2去查重复。

京公网安备 11010502036488号