微软等数据结构与算法面试100题 第七题

第七题


微软亚院之编程判断俩个链表是否相交
给出俩个单向链表的头指针,比如h1,h2,判断这俩个链表是否相交。
为了简化问题,我们假设俩个链表均不带环。

问题扩展:
1.如果链表可能有环列?
2.如果需要求出俩个链表相交的第一个节点列?


分析: 转自:http://blog.sina.com.cn/s/blog_5e3ab00c0100le4s.html


实现代码:

#include<iostream>

using namespace std;

struct linkNode
{
	linkNode * nextLinkNode;

	float value;
};
bool check2LinkList(linkNode * head1, linkNode * head2)
{
	

	bool boolRingList1 = false;
	bool boolRingList2 = false;
	bool boolMutual = false;

	//判断是否存在环
	linkNode * tempLinkNode1 = head1;
	linkNode * tempLinkNode2 = head1;

	while(NULL!=tempLinkNode2)
	{
		tempLinkNode1 = tempLinkNode1->nextLinkNode;
		tempLinkNode2 = tempLinkNode2->nextLinkNode;
		if(NULL!=tempLinkNode2&&NULL!=tempLinkNode2->nextLinkNode)tempLinkNode2 = tempLinkNode2 ->nextLinkNode;
		if(tempLinkNode1==tempLinkNode2)
		{
			boolRingList1 = true;
			break;
		}
	}
	tempLinkNode1 = head2;
	tempLinkNode2 = head2;
	while(NULL!=tempLinkNode2->nextLinkNode)
	{
		tempLinkNode1 = tempLinkNode1->nextLinkNode;
		tempLinkNode2 = tempLinkNode2->nextLinkNode;
		if(NULL!=tempLinkNode2&&NULL!=tempLinkNode2->nextLinkNode)tempLinkNode2 = tempLinkNode2 ->nextLinkNode;
		if(tempLinkNode1==tempLinkNode2)
		{
			boolRingList2 = true;
			break;
		}
	}

	if(boolRingList1&&boolRingList2)
	{
		//都存在环
		//cout<<"both have ring"<<endl;
		int nodeMaxNum = 100;
		int testStart = 0;
		tempLinkNode1 = head1;
		tempLinkNode2 = head2;
		while(testStart<nodeMaxNum)
		{
			tempLinkNode1 = tempLinkNode1 ->nextLinkNode;
			tempLinkNode2 = tempLinkNode2 ->nextLinkNode->nextLinkNode;
			if(tempLinkNode1==tempLinkNode2)
			{
				boolMutual = true;
				cout<<"存在环,有交点"<<endl;
				break;
			}

			
		}

	}
	else if(boolRingList1||boolRingList2)
	{
		//一个存在环 一个不存在环
		cout<<"一个存在环,木有交点"<<endl;
	}
	else
	{
		//不存在环 终点相同
		tempLinkNode1 = head1;
		tempLinkNode2 = head2;
		while(NULL!=tempLinkNode1->nextLinkNode)
			tempLinkNode1 = tempLinkNode1 ->nextLinkNode;
		while(NULL!=tempLinkNode2->nextLinkNode)
			tempLinkNode2 = tempLinkNode2 ->nextLinkNode;
		if(tempLinkNode1==tempLinkNode2)
		{
			boolMutual = true;
			cout<<"不存在环,有交点"<<endl;
		}
	
	}


	return boolMutual;


}
int main()
{

	#pragma region //构建测试链表 head1 head2 head3 head4 head5
	// head1 与 head2 都不存在环 但存在相交 
	// head3 与 head4 存在环 存在相交
	// head3 与 head1: head3存在环 head1 不存在环 不相交
	// head3 与 head5: head3存在环
	//
	//
	linkNode *head1 = new struct linkNode();
	linkNode *b = new struct linkNode();
	linkNode *c = new struct linkNode();
	linkNode *d = new struct linkNode();
	linkNode *e = new struct linkNode();
	

	head1->nextLinkNode = b;
	head1->value = 0;
	b->nextLinkNode = c;
	b->value = 1;
	c->nextLinkNode = d;
	c->value = 2;
	d->nextLinkNode = e;
	d->value = 3;
	e->nextLinkNode = NULL;
	e->value = 4;

	linkNode * temp = head1;
	while(NULL != temp)
	{
		cout<<temp->value<<" ";
		temp = temp->nextLinkNode;
		
	}

	cout<<endl;

	linkNode *head2 = new struct linkNode();
	linkNode *i = new struct linkNode();
	linkNode *j = new struct linkNode();
	linkNode *k = new struct linkNode();
	linkNode *l = new struct linkNode();

	head2->nextLinkNode = i;
	head2->value = 10;
	i->nextLinkNode = j;
	i->value = 11;
	j->nextLinkNode = head1;
	j->value = 12;

	linkNode *head3 = new struct linkNode();
	linkNode *m = new struct linkNode();
	linkNode *n = new struct linkNode();
	linkNode *o = new struct linkNode();
	linkNode *p = new struct linkNode();

	head3->nextLinkNode = m;
	head3->value =21;
	m->nextLinkNode = n;
	m->value = 22;
	n->nextLinkNode = o;
	n->value = 23;
	o->nextLinkNode = p;
	o->value = 24;
	p->nextLinkNode = n;
	p->value = 25;

	linkNode *head4 = new struct linkNode();
	head4 ->nextLinkNode = o;
	head4 ->value = 31;

		temp = head1;
	int maxNum =20;
	int tempindex = 0;
	while(NULL != temp&&tempindex < maxNum)
	{
		tempindex = tempindex + 1;
		cout<<temp->value<<" ";
		temp = temp->nextLinkNode;	
	}
	cout<<endl;
	temp = head2; maxNum =20;tempindex = 0;
	while(NULL != temp&&tempindex < maxNum)
	{
		tempindex = tempindex + 1;
		cout<<temp->value<<" ";
		temp = temp->nextLinkNode;	
	}
	cout<<endl;
	temp = head3; maxNum =20;tempindex = 0;
	while(NULL != temp&&tempindex < maxNum)
	{
		tempindex = tempindex + 1;
		cout<<temp->value<<" ";
		temp = temp->nextLinkNode;	
	}
	cout<<endl;
	temp = head4; maxNum =20;tempindex = 0;
	while(NULL != temp&&tempindex < maxNum)
	{
		tempindex = tempindex + 1;
		cout<<temp->value<<" ";
		temp = temp->nextLinkNode;	
	}
	cout<<endl;
	#pragma endregion


	check2LinkList(head1,head2);
	check2LinkList(head1,head3);
	check2LinkList(head3,head4);
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值