题解 | #两个链表的第一个公共结点#
两个链表的第一个公共结点
https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46
思路
链表
过程
代码
class Solution
{
public:
ListNode* FindFirstCommonNode(ListNode* pHead1, ListNode* pHead2)
{
ListNode* cur1 = pHead1, *cur2 = pHead2;
while(cur1 != cur2)
{
cur1 = (cur1 != nullptr ? cur1->next : pHead2);
cur2 = (cur2 != nullptr ? cur2->next : pHead1);
}
return cur1;
}
};