针对考研的C语言学习(2019链表大题)

题目解析:

【考】双指针算法,逆置法,归并法。

解析:因为题目要求空间复杂度为O(1),即不能再开辟一条链表,因此我们只能用变量来整体挪动原链表。

第一步先找出中间节点

复制代码
typedef NODE* Node;
Node find_mid(Node& h)
{
	Node pre, cur;
	pre = h->next;
	cur = pre;
	while (cur)
	{
		cur = cur->next;
		if (!cur) break;
		cur = cur->next;
		if (!cur) break;
		pre = pre->next;
	}
	Node l2 = (Node)malloc(sizeof(NODE));
	l2->next = pre->next;
	pre->next = NULL;
	return l2;
}

第二步:把找出的中间节点之后的组成的新链表原地逆置

复制代码
void reverse_list(Node& l)
{
	Node s, r, t;
	s = l->next, r = s->next, t = r->next;
	s->next = NULL;
	while (t)
	{
		r->next = s;
		s = r;
		r = t;
		t = t->next;
	}
	r->next = s;
	l->next = r;
}

第三步:合并链表

复制代码
void merge_list(Node& l1, Node& l2)
{
	Node a =  l1->next, b = l2->next;
	Node acur = a;
	a = a->next;
	while (a && b)
	{
		acur->next = b;
		b = b->next;
		acur = acur->next;
		acur->next = a;
		a = a->next;
		acur = acur->next;
	}
	if(!a) acur->next = b;
	
}

【注】以上三步核心算法即为笔试时写的答案

为了让读者看清正确性,我写出完整能运行的代码供参考

复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct LNode {
	ElemType data;
	struct LNode* next;
}NODE;
typedef NODE* Node;

void insert_head_list(Node& l)
{
	ElemType data = 0;
	scanf("%d", &data);
	while (data != 9999)
	{
		Node tmp = (Node)malloc(sizeof(NODE));
		tmp->data = data;
		tmp->next = l->next;
		l->next = tmp;
		scanf("%d", &data);
	}
}


void print_list(Node& l)
{
	Node cur = l->next;
	while (cur)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}


Node find_mid(Node& h)
{
	Node pre, cur;
	pre = h->next;
	cur = pre;
	while (cur)
	{
		cur = cur->next;
		if (!cur) break;
		cur = cur->next;
		if (!cur) break;
		pre = pre->next;
	}
	Node l2 = (Node)malloc(sizeof(NODE));
	l2->next = pre->next;
	pre->next = NULL;
	return l2;
}
void reverse_list(Node& l)
{
	Node s, r, t;
	s = l->next, r = s->next, t = r->next;
	s->next = NULL;
	while (t)
	{
		r->next = s;
		s = r;
		r = t;
		t = t->next;
	}
	r->next = s;
	l->next = r;
}

void merge_list(Node& l1, Node& l2)
{
	Node a =  l1->next, b = l2->next;
	Node acur = a;
	a = a->next;
	while (a && b)
	{
		acur->next = b;
		b = b->next;
		acur = acur->next;
		acur->next = a;
		a = a->next;
		acur = acur->next;
	}
	if(!a) acur->next = b;
	
}
int main()
{
	Node l = (Node)malloc(sizeof(NODE)); //存储头节点的头指针
	l->next = NULL;
	insert_head_list(l);//头插法
	print_list(l);
	Node l2 = find_mid(l);
	/*print_list(l);*/
	print_list(l2);
	reverse_list(l2);
	print_list(l2);
	merge_list(l, l2);
	print_list(l);
	return 0;
}

运行结果图片

链表长度为奇数时

链表长度为偶数时

相关推荐
X-future4262 分钟前
OpenClaw源码学习
学习
_JoeZoe10 分钟前
阿里云开发者社区用户服务协议
c语言·编译器·历史·应用领域·unix操作系统
老王爱玩车13 分钟前
第2讲:C语言数据类型和变量
c语言·开发语言
kaixin_啊啊14 分钟前
中国研究生数学建模竞赛(华为杯)学习笔记——三维瀑布图绘图代码
笔记·学习·数学建模
动词ing22 分钟前
【学习笔记】数据结构(链表合并 双指针合并有序链表)
数据结构·笔记·学习
风之清扬27 分钟前
Agent学习之四-初识Agent CLI
人工智能·科技·学习
传奇开心果编程30 分钟前
【xilem0.4基础语法学与练】第45课 xilem_core
学习·rust·前端框架
Vcaker30 分钟前
Linux学习28-Kubernetes service
linux·运维·学习
零依赖极客34 分钟前
Day 9·1 KV 也量化——q8 KV 把缓存与 decode 带宽压到一半
c语言·开发语言·arm开发·人工智能·缓存·矩阵
那年窗外下的雪.1 小时前
AIDC 学习日志|第 25 天|设备输出反推、MAC Flapping 与 EAD 撤销
前端·网络·git·学习·macos