面试题:如何快速查找未知长度单链表的中间节点?

面试题:如何快速查找未知长度单链表的中间节点?

背景

偶然接触到一面试题:如何快速查找未知长度单链表的中间节点?

方法

首先想到就是很简单的遍历方法,然后又得知一种快慢指针的方法。

普通方法

原理

普通方法很简单,首先遍历一遍单链表以确定单链表的长度。然后再次从头节点出发循环N/2次即可找到单链表的中间节点。

算法的复杂度为O(N+N/2)=O(3N/2)。

代码
void common_found_List(Linklist *L,int len)
{
    
    
	Linklist *mid;
	int i = 0;
	Elemtype e;
	mid = L;
	len = len/2;
	while(i<=len)
	{
    
    
		mid = mid->next;
		i++;
	}
	e = mid->data;
	printf("The middle data of the list is :%d\n",e);
}

高级方法

优化普通查找方法→★★★快慢指针★★★

原理

设置两个指针p和mid都指向单链表的头节点。其中p的移动速度是mid的2倍。当*p指向末尾节点的时候,*mid也将正好指向中间节点。

算法的复杂度为O(N/2)。

代码
void fast_slow_List(Linklist *L)
{
    
    
	Linklist *f,*mid;
	Elemtype e;

	f = L;
	mid = L;

	while(f->next)
	{
    
    
		if(f->next->next)
		{
    
    
			f = f->next->next;
			mid = mid->next;
		}
		else
		{
    
    
			f = f->next;
		}
	}
	e = mid->next->data;
	printf("The middle data of the list is :%d\n",e);
}

实验过程及结果

总代码
//<库函数>
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<math.h>

//<相关参数的宏定义>
#define Len sizeof(Node)
#define N 20                              
typedef int Elemtype;

typedef struct Node
{
    
    
	Elemtype data;
	struct Node* next;
}Node;

typedef struct Node Linklist;

//功能函数
Linklist* Creat_L();
void Out_Linklist(Linklist *h);
void fast_slow_List(Linklist *L);
void common_found_List(Linklist *L,int len);
int Length_list(Linklist *L);

void main()
{
    
    
	Linklist *L;
	int k ;
	int i ;
	int len;
	Elemtype x;

	printf("		《 Welcome to use the founction 》\n");
	printf("	    The founction can offer following service\n");
	printf("		*******Please first Initlist*******\n\n");
	printf("		1、Initlist          ->创建单链表\n");
	printf("		2、Insertlist        ->输出链表长度操作\n");
	printf("		3、common_Foundlist  ->普通方法查找中间元素操作\n");
	printf("		4、fast_Foundlist    ->高级方法查找中间元素操作\n");
	printf("		0、Exitlist          ->退出操作\n\n\n");
	printf("        Please choose following digital:1、2、3、0\n");
	printf("               Finish your idea!!!!!!\n");
	do
	{
    
    
		printf("\n\nPlease Input your choice:");
		scanf("%d",&k);
		switch(k)
		{
    
    
		case 1:
			{
    
    
				L = Creat_L(); 
				Out_Linklist(L);
			}break;
		case 2:
			{
    
    
				len = Length_list(L);
				printf("The length of the list is:%d\n",len);
			}break;
		case 3:
			{
    
    
				len = Length_list(L);
				common_found_List(L,len);
			}break;
		case 4:
			{
    
    
				fast_slow_List(L);
				Out_Linklist(L);
			}break;
		}
	}while(k!=0);
}

// <随机创建单链表>
Linklist* Creat_L()
{
    
    
	int count = 0;
	Linklist *h,*p,*s;
	Elemtype x;
	h = (Node*)malloc(Len);
	h->next = NULL;	
	p = h;
	//printf("Please Enter the FIRST Node data: ");
	//scanf("%d",&x);
	while(count < N)                          //假设终止条件
	{
    
    
		x = rand() % 50;                      //随机生成各节点,创建单链表
		s = (Node*)malloc(Len);	
		s->data = x;                          //赋值		
		s->next = NULL;
		p->next = s;
		p = s;	
		//printf("Please Enter the NEXT  Node data: ");
		//scanf("%d",&x);
		count++;
	}
	return h;
}

//<输出函数>
void Out_Linklist(Linklist *h)
{
    
    
	Linklist *p;
	p  = h->next;
	printf("Output Linklist:\n");
	while(p)
	{
    
    
		if(p->next!=NULL)
		{
    
    
			printf("%d -> ",p->data);
			p = p->next;
		}
		else
		{
    
    
			printf("%d",p->data);
			p = p->next;
		}
	}
}

// <快慢指针查找函数:使用快慢指针查找中间值的操作,时间复杂度为O(N/2)>
void fast_slow_List(Linklist *L)
{
    
    
	Linklist *f,*mid;
	Elemtype e;

	f = L;
	mid = L;

	while(f->next)
	{
    
    
		if(f->next->next)
		{
    
    
			f = f->next->next;
			mid = mid->next;
		}
		else
		{
    
    
			f = f->next;
		}
	}
	e = mid->next->data;
	printf("The middle data of the list is :%d\n",e);
}

//<长度函数>
int Length_list(Linklist *L)
{
    
    
	int count = 0;
	Linklist *p;

	p = L;
	while(p->next)
	{
    
    
		p = p->next;
		count++;
	}
	//printf("The length of the list is:%d\n",count);
	return count;
}

// <普通方法函数>
void common_found_List(Linklist *L,int len)
{
    
    
	Linklist *mid;
	int i = 0;
	Elemtype e;
	mid = L;
	len = len/2;
	while(i<=len)
	{
    
    
		mid = mid->next;
		i++;
	}
	e = mid->data;
	printf("The middle data of the list is :%d\n",e);
}
运行结果

在这里插入图片描述

总结

利用快慢指针算法查找单链表的中间节点相比普通算法速度提高了3倍左右。当然,本例看不出速度差距,元素较多时,会出现较为明显的差距。

另外,高级方法会在你的面试时加分哦!!!!!!

猜你喜欢

转载自blog.csdn.net/MZYYZT/article/details/113437641