两个单向链表,找出它们的第一个公共结点

本文探讨了如何找出两个单向链表的第一个公共节点,并提供了一种线性时间复杂度的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:两个单向链表,找出它们的第一个公共结点。

链表的结点定义为:

struct ListNode

{

      int         m_nKey;

      ListNode*   m_pNext;

};

分析:这是一道微软的面试题。微软非常喜欢与链表相关的题目,因此在微软的面试题中,链表出现的概率相当高。

如果两个单向链表有公共的结点,也就是说两个链表从某一结点开始,它们的m_pNext 都指向同一个结点。但由于是单向链表的结点,每个结点只有一个m_pNext ,因此从第一个公共结点开始,之后它们所有结点都是重合的,不可能再出现分叉。所以,两个有公共结点而部分重合的链表,拓扑形状看起来像一个Y ,而不可能像X

看到这个题目,第一反应就是蛮力法:在第一链表上顺序遍历每个结点。每遍历一个结点的时候,在第二个链表上顺序遍历每个结点。如果此时两个链表上的结点是一样的,说明此时两个链表重合,于是找到了它们的公共结点。如果第一个链表的长度为m ,第二个链表的长度为n ,显然,该方法的时间复杂度为O(mn)

接下来我们试着去寻找一个线性时间复杂度的算法。我们先把问题简化:如何判断两个单向链表有没有公共结点?前面已经提到, 如果两个链表有一个公共结点,那么该公共结点之后的所有结点都是重合的。那么,它们的最后一个结点必然是重合的。因此,我们判断两个链表是不是有重合的部 分,只要分别遍历两个链表到最后一个结点。如果两个尾结点是一样的,说明它们用重合;否则两个链表没有公共的结点。

在上面的思路中,顺序遍历两个链表到尾结点的时候,我们不能保证在两个链表上同时到达尾结点。这是因为两个链表不一定长度一样。但如果假设一个链表比另一个长l 个结点,我们先在长的链表上遍历l 个结点,之后再同步遍历,这个时候我们就能保证同时到达最后一个结点了。由于两个链表从第一个公共结点开始到链表的尾结点,这一部分是重合的。因此,它们肯定也是同时到达第一公共结点的。于是在遍历中,第一个相同的结点就是第一个公共的结点。

在这个思路中,我们先要分别遍历两个链表得到它们的长度,并求出两个长度之差。在长的链表上先遍历若干次之后,再同步遍历两个链表,直到找到相同的结点,或者一直到链表结束。此时,如果第一个链表的长度为m ,第二个链表的长度为n ,该方法的时间复杂度为O(m+n)

基于这个思路,我们不难写出如下的代码:

///

// Find the first common node in the list with head pHead1 and

// the list with head pHead2

// Input: pHead1 - the head of the first list

//        pHead2 - the head of the second list

// Return: the first common node in two list. If there is no common

//         nodes, return NULL

///

ListNode * FindFirstCommonNode( ListNode *pHead1, ListNode *pHead2)

{

      // Get the length of two lists

      unsigned int nLength1 = ListLength(pHead1);

      unsigned int nLength2 = ListLength(pHead2);

      int nLengthDif = nLength1 - nLength2;

 

      // Get the longer list

      ListNode *pListHeadLong = pHead1;

      ListNode *pListHeadShort = pHead2;

      if(nLength2 > nLength1)

      {

            pListHeadLong = pHead2;

            pListHeadShort = pHead1;

            nLengthDif = nLength2 - nLength1;

      }

 

      // Move on the longer list

      for(int i = 0; i < nLengthDif; ++ i)

            pListHeadLong = pListHeadLong->m_pNext;

 

      // Move on both lists

      while((pListHeadLong != NULL) &&

            (pListHeadShort != NULL) &&

            (pListHeadLong != pListHeadShort))

      {

            pListHeadLong = pListHeadLong->m_pNext;

            pListHeadShort = pListHeadShort->m_pNext;

      }

 

      // Get the first common node in two lists

      ListNode *pFisrtCommonNode = NULL;

      if(pListHeadLong == pListHeadShort)

            pFisrtCommonNode = pListHeadLong;

 

      return pFisrtCommonNode;

}

 

///

// Get the length of list with head pHead

// Input: pHead - the head of list

// Return: the length of list

///

unsigned int ListLength(ListNode* pHead)

{

      unsigned int nLength = 0;

      ListNode* pNode = pHead;

      while(pNode != NULL)

      {

            ++ nLength;

            pNode = pNode->m_pNext;

      }

 

      return nLength;

}

已知两个公共点的无头结点单向链表,寻找它们的第一个公共结点的问题通常通过“快慢指针”算法来解决。在这个算法中,我们维护两个指针,一个移动得较快(通常是原地步进两步),另一个移动较慢(一步)。当快指针追上慢指针时,说明它们已经相遇过一次,此时慢指针所在的位置就是公共部分的起点。如果快指针还没有超过链表长度,则继续让慢指针前进,直到再次追上快指针,这就是第一个公共点。 以下是Python的一个简单实现: ```python def findFirstCommonNode(list1, list2): if not list1 or not list2: return None # 如果其中一个链表为空,直接返回None slow_ptr1 = list1.head slow_ptr2 = list2.head fast_ptr1 = list1.head fast_ptr2 = list2.head while fast_ptr1 and fast_ptr1 != slow_ptr2: fast_ptr1 = fast_ptr1.next if fast_ptr1 else None fast_ptr2 = fast_ptr2.next if fast_ptr2 else None slow_ptr2 = slow_ptr2.next if slow_ptr2 else None if fast_ptr1 is None or fast_ptr2 is None or fast_ptr1 != fast_ptr2: return None # 指针未交叉则无公共点 # 现在找到交点,从交点开始遍历,直到找到相同的点 common_ptr = fast_ptr1 while common_ptr and common_ptr.next: common_ptr1 = list1.head common_ptr2 = list2.head while common_ptr1 and common_ptr1 != common_ptr: common_ptr1 = common_ptr1.next while common_ptr2 and common_ptr2 != common_ptr: common_ptr2 = common_ptr2.next if common_ptr1 == common_ptr2: return common_ptr common_ptr = common_ptr.next return None # 没有找到公共点 # 使用示例: # 假设list1和list2的头结点分别为node1和node2,这里省略实际链表创建过程 first_common_node = findFirstCommonNode(list1, list2) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值