程序员面试问题(七)

算法部分(4)

4.判断两个链表是否相交。
思路:对于链表相交,可见下图,若两链表有一个公共元素,则后面元素都相同。
两条链表L1与L2相交,红色为相交后的公共部分
1.暴力解法。遍历L2,查找L2的元素是否属于L1。时间复杂度O(NM),其中N,M分别为L1,L2的长度。
2.暴力解法优化。将L1所有节点的地址储存起来,假设使用哈希存储或者平衡树,会有较快的插入和检索速度,然后检查L2中的节点地址是否在L1中。假设插入检索算法时间复杂度都是 O(logN),则总的时间复杂度为 O((N+M)logN)。但是要存储地址,空间复杂度O(N).
3.检测环法。考虑将L2接到L1后面,如图:

这样,就会形成一个环,只要检查有没有环存在,就能知道两链表是否相交。关于链表是否存在环的问题请看
如此,时间复杂度为 O(N +M),空间复杂度O(1).

灼爷的代码:
#include <stdio.h>
#include <stdlib.h>
struct Node{
	int element;
	struct Node* next;
};
typedef struct Node *PtrToNode;
typedef	PtrToNode Position;
typedef PtrToNode List;
void MakeEmpty(List l)
{
	l->next =NULL;
}
// insert after the head
List Insert(int x,List l)
{
	Position tmpCell;
	tmpCell =(Position)malloc(sizeof(struct Node));
	if(tmpCell ==NULL)
		{
			printf("out of space ");
			return NULL;
		}
		else
		{
		tmpCell->element = x;
		tmpCell->next =l->next;
		l->next =tmpCell;
		return l;
	}
		
}
void print(List l)
{
	Position k =l->next;
	while(k)
	{
		printf("%d\t",k->element);
		k =k->next;
	}
}
List reserve(List l)
{
	if(!l->next)
	{
		printf("an empty list");
		return NULL;
	}
	else
	{
		Position preNode,curNode,nextNode;
		preNode =NULL;
		curNode =l->next;
		nextNode =curNode->next;
		while(nextNode !=NULL)
		{
			curNode->next =preNode;
			preNode =curNode;
			curNode =nextNode;
			nextNode =nextNode->next;
		}
		curNode->next =preNode;
		l->next =curNode;
		return l;
	}
}
Position Find(int x,List l)
{
	Position p;
	p =l->next;
	while(p)
	{
		if(p->element ==x)
			break;
		p =p->next;
	}
	return p;
}
Position findLast(List l)
{
	Position p;
	p =l;
	while(p->next)
	{
		p =p->next;
	}
	return p;
}
void DeleteList(List l)
{
	Position p,tmp;
	p =l->next;
	l->next =NULL;
	while(p !=NULL)
	{
		tmp =p->next;
		free(p);
		p =tmp;
	}
} 
//将可用的链表变成坏链表 ,使尾指针指向某中间节点 
void MakeLoop(int x,List l)
{
	Position p =findLast(l);
	p ->next =Find(x,l);
}

Position get2Next(Position p,List l)
{
	p =p->next;
	if(p)
		{
			p =p->next;
			return p;
		}
	else
		return NULL;
}
int IsLoop1(List l)
{
	int flag =0;
	Position slow,fast,t;
	slow =l;
	fast =l;
	while((fast->next) && (fast->next->next))
	{

		while(fast->next)
		{
			fast =get2Next(fast,l);
			slow =slow->next;
			if(fast ==slow)
			{
				flag =1;
				break;
			}
		}
		if(flag)
			break;
	}
	return flag;
} 
void MakeAssociate(List l1,List l2)
{
	Position p,q;
	p =l1;
	q =l2;
	while(p->next && q->next)	
	{
		p =p->next;
		q =q->next;
	}
	if(p->next)
		q->next =p;
	else
		p->next =q;
}
int IsAssociate(List l1,List l2)
{
	Position p,q;
	p =l1;
	q =l2;
	int flag=0;
	while((p->next) && (q->next))
	{
		p =p->next;
		q =q->next;
	}
	if(p->next)
		{
			q->next =l1->next;
			flag =IsLoop1(l2);}
	else
		{
			p->next =l2->next;
			flag =IsLoop1(l1);}
	if(flag)
		return 1;
	else
		return 0;
		
	
}
int main()
{
	List lp =(List)malloc(sizeof(struct Node));
	List lq =(List)malloc(sizeof(struct Node));
	MakeEmpty(lp);
	MakeEmpty(lq);
	for(int i =20;i>0;i--)
		Insert(i,lp);
	for(int i =8;i>0;i--)
		Insert(i,lq);
	printf("在list1中插入一些值\n");
	print(lp);
	printf("在list2中插入一些值\n");
	print(lq);
	//MakeAssociate(lp,lq);
	printf("\n");
	printf("如果使链表相交,则输出长度增加的链表,节点值:\n");
	print(lq);
	printf("\n");
	int k =IsAssociate(lp,lq);
	printf("输出1 为 相交链表,输出0 为非相交链表\n");
	printf("%d\t",k);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值