剑指Offer:面试题37 两个链表的第一个公共结点

/*
两个链表的第一个公共结点:
输入两个链表,找出它们的第一个公共结点。、

分析:
这是数据结构的一道题目。
设长链表长度为L=5,
短链表长度为S = 3,
那么先让长链表走L-S= 2步,
那么长链表从第3个节点开始,短链表从第一个节点开始,依次比较即可。

输入:
输入可能包含多个测试样例。
对于每个测试案例,输入的第一行为两个整数m和n(1<=m,n<=1000):代表将要输入的两个链表的元素的个数。
接下来的两行,第一行为第一个链表的所有元素,中间用空格隔开。第二行为第二个链表的所有元素,中间用空格隔开。
输出:
对应每个测试案例,
输出两个链表的第一个公共结点的值。
如果两个链表没有公共结点,则输出“My God”。
样例输入:
5 4
1 2 3 6 7
4 5 6 7

3 3
1 5 7
2 4 7

2 3
1 3
4 5 6
样例输出:
6
7
My God
*/

#include <stdio.h>
#include <string.h>
const int MAXSIZE = 1001;
#define INF 1000000000;
typedef struct Node
{
 int _iVal;
 struct Node* _next;
}Node;

//实在不想删除节点,用静态数组
Node nodeArr1[MAXSIZE];
int _iIndex1;
Node nodeArr2[MAXSIZE];
int _iIndex2;

Node* createNode1()
{
 return &nodeArr1[_iIndex1++];
}

Node* createNode2()
{
 return &nodeArr2[_iIndex2++];
}

void buildList(Node** pHead,int* pArr,int n,Node* (*pFun)())
{
 if(!pHead || !(*pHead))
 {
  return;
 }
 Node* pNode,*pCurNode;
 for(int i = 0 ; i < n ; i++)
 {
  if(i)//尾插法建立单链表
  {
   pNode = pFun();//动态策略
   pNode->_iVal = pArr[i];
   pNode->_next = NULL;
   pCurNode->_next = pNode;
   pCurNode = pNode;
  }
  else
  {
   pCurNode = *pHead;
   pCurNode->_iVal = pArr[i];
   pCurNode->_next = NULL;
  }
 }
}

int firstCommonNode(Node* pHead1,int iLen1,Node* pHead2,int iLen2)
{
 if(!pHead1 || !pHead2)
 {
  return INF;
 }
 Node* pLongList,*pShortList;
 if(iLen1 > iLen2)//如果链表1是长链表
 {
  pLongList = pHead1;
  pShortList = pHead2;
 }
 else
 {
  pLongList = pHead2;
  pShortList = pHead1;
 }
 int maxLen = iLen1 > iLen2 ? iLen1 : iLen2;
 int minLen = iLen1 + iLen2 - maxLen;
 int diffLen = maxLen - minLen;
 while(diffLen-- && pLongList)//接下来,让长链表先多diffLen步
 {
  pLongList = pLongList->_next;
 }
 while(pLongList && pShortList)
 {
  if(pLongList->_iVal == pShortList->_iVal)
  {
   return pLongList->_iVal;
  }
  else
  {
   pLongList = pLongList->_next;
   pShortList = pShortList->_next;
  }
 }
 return 1000000000;
}

void process()
{
 int n,m;
 while(EOF != scanf("%d %d",&n,&m))
 {
  if(n < 1 || n >= MAXSIZE || m < 1 || m >= MAXSIZE)
  {
   continue;
  }
  int iList1Arr[MAXSIZE];
  int iList2Arr[MAXSIZE];
  for(int i = 0; i < n; i++)
  {
   scanf("%d",&iList1Arr[i]);
  }
  for(int j = 0 ; j < m ; j++)
  {
   scanf("%d",&iList2Arr[j]);
  }
  memset(nodeArr1,NULL,sizeof(nodeArr1));
  memset(nodeArr2,NULL,sizeof(nodeArr2));
  _iIndex1 = 0;
  _iIndex2 = 0;
  Node* head1 =  createNode1();
  Node** pHead1 = &head1;
  Node* head2 = createNode2();
  Node** pHead2 = &head2;
  buildList(pHead1,iList1Arr,n,createNode1);
  buildList(pHead2,iList2Arr,m,createNode2);
  int iRes = firstCommonNode(head1,n,head2,m);
  if(iRes != 1000000000)
  {
   printf("%d\n",iRes);
  }
  else
  {
   printf("My God\n");
  }
 }
}

int main(int argc,char* argv[])
{
 process();
 getchar();
 return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值