/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } };*/ class Solution { public: ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) { // 都为空就不往下了 if (pHead1 == nullptr || pHead2 == nullptr) return nullptr; // 定义两个链表指针,分别指向头结点 ListNode* l1 = pHead1, * l2 = pHead2; // 通过这个循环,先走一遍“自家野区”,再走一遍“敌方野区” while (l1 != l2) { if (l1 != nullptr) l1 = l1->next; else l1 = pHead2; if (l2 != nullptr) l2 = l2->next; else l2 = pHead1; } // 返回重合处的指针 return l1; } };