找出两个链表的第一个公共节点
来源:互联网 发布:java easyui使用 编辑:程序博客网 时间:2024/05/24 07:42
问题描述:给定两个单向链表,找出它们的第一个公共节点。链表的节点定义如下:
struct ListNode
{
int m_nKey;
ListNode* m_pNext;
};
问题分析:这是一道微软的面试题。微软非常喜欢与链表相关的题目,因此在微软的面试题中,链表出现的概率非常高。如果两个单向链表有公共的节点,也就是说两个链表从某一个节点开始,它们的m_pNext都指向同一个节点。但由于是单向链表的节点,每个节点只有一个m_pNext,因此从第一个公共节点开始,之后它们所有的节点都是重合的,不可能出现分叉。所以,两个有公共结点而部分重合的链表,拓扑形状看起来像一个Y,而不可能像X。
在上面的思路中,顺序遍历两个链表到尾结点的时候,我们不能保证在两个链表上同时到达尾结点。这是因为两个链表长度不一定一样。但我们假设一个链表比另一个长l个结点,我们先在长的链表上遍历l个结点,之后再同步遍历两个链表,这个时候我们就能保证两个链表同时到达尾节点了。由于两个链表从第一个公共结点开始到链表的尾结点,这一部分是重合的。因此,它们肯定也是同时到达第一公共结点的。于是在遍历中,第一个相同的结点就是第一个公共的结点。
在这个思路中,我们先要分别遍历两个链表得到它们的长度,并求出两个链表的长度之差。在长的链表上先遍历若干次之后,再同步遍历两个链表,直到找到相同的结点,或者一直到链表结束。此时,如果第一个链表的长度为m,第二个链表的长度为n,该方法的时间复杂度为O(m+n)。
#include <stdio.h>
#include <malloc.h>#include <stdlib.h>
typedef struct tagListNode
{
int m_nKey;
struct tagListNode* m_pNext;
}ListNode;
unsigned int ListLength(ListNode* pHead);
ListNode* FindFirstCommonNode(ListNode* pHead1, ListNode* pHead2)
{
unsigned int nLength1=ListLength(pHead1);//第一个链表长度
unsigned int nLength2=ListLength(pHead2);//第二个链表长度
int nLengthDif=nLength1-nLength2;//两个链表长度差
ListNode* pListHeadLong=pHead1;
ListNode* pListHeadShort=pHead2;
if (nLength2>nLength1)
{
pListHeadLong=pHead2;
pListHeadShort=pHead1;
nLengthDif=nLength2-nLength1;
}
for (int i=0;i<nLengthDif;i++)//长链表先遍历长度差次
{
pListHeadLong=pListHeadLong->m_pNext;
}
while (pListHeadLong!=NULL && pListHeadShort!=NULL && pListHeadShort!=pListHeadLong)
{ //两个链表同步遍历,直到公共节点结束
pListHeadLong=pListHeadLong->m_pNext;
pListHeadShort=pListHeadShort->m_pNext;
}
ListNode* pFirstCommonNode = NULL;
if (pListHeadLong == pListHeadShort)
{
pFirstCommonNode=pListHeadLong;
}
return pFirstCommonNode;
}
unsigned int ListLength(ListNode* pHead)
{
unsigned int nLength=0;
while (pHead!=NULL)
{
++nLength;
pHead=pHead->m_pNext;
}
return nLength;
}
void ListPrint(ListNode* pHead)
{
if (pHead == NULL)
{
printf("List is Null!/n");
}
while(pHead->m_pNext != NULL)
{
printf("%d -> ",pHead->m_nKey);
pHead=pHead->m_pNext;
}
printf("%d/n",pHead);
}
int main(int argc, char** argv)
{
ListNode* pHead1, *pHead2;
int i;
pHead1=(ListNode*)malloc(sizeof(ListNode)*5);
for (i=0;i<4;i++)
{
pHead1[i].m_nKey=rand();
pHead1[i].m_pNext=pHead1+i+1;
}
pHead1[4].m_nKey=4;
pHead1[4].m_pNext=NULL;
pHead2=(ListNode*)malloc(sizeof(ListNode)*3);
pHead2->m_nKey=rand();
pHead2->m_pNext=pHead2+1;
pHead2[1].m_nKey=rand();
pHead2[1].m_pNext=pHead2+2;
pHead2[2].m_nKey=rand();
pHead2[2].m_pNext=pHead1->m_pNext;
ListPrint(pHead1);
ListPrint(pHead2);
printf("The first common node's m_nkey is %d/n",FindFirstCommonNode(pHead1,pHead2)->m_nKey);
free(pHead1);
free(pHead2);
return 0;
}
0 0
- 找出两个链表的第一个公共节点
- 找出两个链表的第一个公共节点
- Java找出两个链表的第一个公共节点
- 找出两个链表的第一个公共节点
- 输入两个链表,找出他们第一个公共节点
- 程序员面试题精选——找出两个链表的第一个公共节点
- 输入两个链表,找出它们的第一个公共节点(值)
- 面试题04:找出两个单向链表的第一个公共节点
- 找出二叉树中某两个节点的第一个公共祖先
- 两个链表的公共第一个公共节点
- 找出两个链表的第一个公共结点--总结
- 找出两个链表的第一个公共结点
- 找出两个链表的第一个公共结点
- 找出两个链表的第一个公共结点
- 找出两个链表的第一个公共结点
- 找出两个链表的第一个公共结点
- 找出两个链表的第一个公共结点
- 找出两个链表的第一个公共结点
- Oracle ERP 库存管理(业务流程 核心流程)
- dev控件apsxgridview记录2
- win7系统忘记开机密码怎么办?
- POJ 3281 Dining(最大流)
- 8-3. 组个最小数
- 找出两个链表的第一个公共节点
- ios中处理时间的类及方法详解
- query specified join fetching, but the owner of the fetched association was not present in the selec
- Javascript 学习 笔记一
- iOS运行出现No application was specified 和 IOS 模拟器未能安装此应用程序
- zoj 3675 状压dp
- 对功率谱的一点理解
- IOS APP 上传到AppStore
- 前台页面优化全攻略(一)