两链表的第一个公共结点

来源:互联网 发布:qq邮箱pop3端口 编辑:程序博客网 时间:2024/05/17 09:17

转自:http://zhedahht.blog.163.com/blog/static/254111742008053169567/

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

链表的结点定义为:

struct ListNode{    int m_nKey;    ListNode*   m_pNext;};

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

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

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

在上面的思路中,顺序遍历两个链表到尾结点的时候,我们不能保证在两个链表上同时到达尾结点。这是因为两个链表不一定长度一样。但如果假设一个链表比另一个长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 intnLength1 = ListLength(pHead1);

      unsigned intnLength2 = ListLength(pHead2);

      ListNode *pListHeadLong = NULL;

      ListNode *pListHeadShort = NULL;


      // Get the longer list

      if(nLength2 >nLength1)

      {

            pListHeadLong = pHead2;

            pListHeadShort = pHead1;

            nLengthDif = nLength2 - nLength1;

      }

      else

      {

            pListHeadLong = pHead1;

            pListHeadShort = pHead2;

            nLengthDif = nLength1 - nLength2;

      }

      // 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 intnLength = 0;

      ListNode* pNode = pHead;

      while(pNode !=NULL)

      {

            ++nLength;

            pNode = pNode->m_pNext;

      }

      return nLength;

}

0 0
原创粉丝点击