针对考研的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;
}

运行结果图片

链表长度为奇数时

链表长度为偶数时

相关推荐
xwz小王子1 小时前
PerAct2:机器人双臂操作任务的基准测试和学习
学习·机器人
数据最前线2 小时前
数据管理技术发展的3个阶段
数据库·考研·数据库系统概论
d111111111d2 小时前
STM32外设学习--DMA直接存储器读取--学习笔记。
笔记·stm32·单片机·嵌入式硬件·学习
培林将军2 小时前
Visual Studio Code 之C/C++开发编译环境搭建
c语言·c++·vscode
on_pluto_2 小时前
【推荐系统14】数据分析:以阿里天池新闻推荐为例学习
人工智能·学习·数据挖掘·数据分析·推荐算法
数学难2 小时前
数据分析学习路线
学习·数据挖掘·数据分析
搞机械的假程序猿3 小时前
普中51单片机学习笔记-前言
笔记·学习·51单片机
宋辰月3 小时前
学习react第一天
javascript·学习·react.js
笨鸟笃行3 小时前
百日挑战——单词篇(第十八天)
学习
好奇龙猫3 小时前
日语学习-日语知识点小记-构建基础-JLPT-N3阶段-二阶段(16):文法和单词-第四课
学习