/** * struct ListNode { * int val; * struct ListNode *next; * }; */ /** * * @param pHead1 ListNode类 * @param pHead2 ListNode类 * @return ListNode类 */ struct ListNode* FindFirstCommonNode(struct ListNode* pHead1, struct ListNode* pHead2 ) { // write code here if(pHead1 == NULL || pHead2 == NULL) return NULL; int l1 = 0, l2 = 0; struct ListNode *p1 = pHead1, *p2 = pHead2; while(p1 != NULL) { l1++; p1 = p1->next; } while(p2 != NULL) { l2++; p2 = p2->next; } p1 = pHead1; p2 = pHead2; if(l1 > l2) { int n = l1 - l2; while(n > 0) { n--; p1 = p1->next; } } else { int n = l2 - l1; while(n > 0) { n--; p2 = p2->next; } } while(p1 != NULL && p2 != NULL && p1 != p2) { p1 = p1->next; p2 = p2->next; if(p1 == p2) break; } if(p1 == p2) { return p1; } else { return NULL; } }