题解 | #两个链表的第一个公共结点#
两个链表的第一个公共结点
https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46
class Solution { public: ListNode* FindFirstCommonNode( ListNode* head1, ListNode* head2) { ListNode* cur1 = head1; ListNode* cur2 = head2; while(cur1 != cur2) { if(cur1) cur1=cur1->next; else cur1 = head2; if(cur2) cur2=cur2->next; else cur2 = head1; } return cur1; } }; class Solution { public: ListNode* FindFirstCommonNode( ListNode* head1, ListNode* head2) { ListNode* cur1 = head1; ListNode* cur2 = head2; while(cur1 != cur2) { cur1 = cur1?cur1->next:head2; cur2 = cur2?cur2->next:head1; } return cur1; } };