7-2 Reversing Linked List

浙大PAT反转链表:7-2 Reversing Linked List 7-2 Reversing Linked List (25分) Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you ... 阅读详情

Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must output 4→3→2→1→5→6.

Input Specification:

Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤10 5 ) which is the total number of nodes, and a positive K (≤N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

Then N lines follow, each describes a node in the format:

Address Data Next

where  Address is the position of the node, Data is an integer, and  Next is  the position of the next node.

Output Specification:

For each case, output the resulting ordered linked list. Each node occupies a line and is printed in the same format as in the input.

Sample Input:

00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

Sample Output:

00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1

 

       琢磨了好久才写出来的代码,没想到还是不完美【唉】,因为输入数据可以最多达到100000个,我的代码调用链表函数过多,导致时间复杂性过高,小部分样例就运行超时了【难受】,不过程序整体框架没问题,好歹25分满分也拿了22分【哈哈】。

#include<stdio.h>
#include<stdlib.h>

typedef struct Node List;
struct Node{
	int Addr;
	int Data;
	int NextAddr;
	List *Next; 
};

int num = 0;  //链表建好之后的链表节点数

List *ReadList(int N);
List *CreateList(List *L,int firstaddr,int N);
List *ListReverse(List *head,int K);
void PrintList(List *L);

int main()
{
	int N,K,FirstAddr;
	List *Read,*a;
	scanf("%d %d %d",&FirstAddr,&N,&K);
    Read = ReadList(N);
	a = CreateList(Read,FirstAddr,N);

	List *p = a;                                  //p指向链表头结点
    List *rp = NULL;                              //反转链表函数的返回值
	if(K == 1){
		PrintList(a);
	}else if(K <= num){
		for(int i=0; i<(num/K); i++){
			rp = ListReverse(p,K);
			p->Next = rp;            // 使p->Next 指向反转后链表的第一个节点
            p->NextAddr = rp->Addr;  // 更改NextAddr值,指向逆转后链表的下一个节点的Addr
            
			int j = 0;
			/*使p指向下一段需要反转的子链表的头结点(第一个节点的前一个节点)*/
            while (j < K){
                p = p->Next;
                j++;
            }
		}
    	PrintList(a);   //输出反转后链表
	}
	return 0;
 } 

List *ReadList(int N)  /*用单链表存储数据*/
{
	List *head,*rear;
	head = (List *)malloc(sizeof(struct Node));  //头结点 
	rear = head;              //rear始终指向终端结点,开始时指向头结点
	while( N-- ){
		List *p;
		p = (List *)malloc(sizeof(struct Node));
		scanf("%d %d %d",&(p->Addr),&(p->Data),&(p->NextAddr));
		rear->Next = p;
		rear = p;
	}
	rear->Next = NULL;
	
	return head;
}
 
List *CreateList(List *L,int firstaddr,int N)  //建立有顺序的单链表(尾插法) 
{
	int nextaddr = firstaddr;
	List *head,*rear,*p;
	head = (List *)malloc(sizeof(struct Node));  //头结点 
	rear = head;         //rear始终指向终端结点,开始时指向头结点
	
	while((N--) && (nextaddr != -1)){
		p = L->Next;     //每次都从链表头开始遍历 
		while( p ){
			if( p->Addr == nextaddr ){  /*若找到对应的下一结点*/
			    num++;
				List *q = (List *)malloc(sizeof(struct Node));  //申请结点空间 
				q->Addr = p->Addr;            //数据域赋值 
				q->Data = p->Data;
				q->NextAddr = p->NextAddr;
				rear->Next = q;               //尾插法 
				rear = q;
				break;
			}else{
				p = p->Next;         //没找到,链表指针后移 
			}
		}
		nextaddr = rear->NextAddr;     //每找到一个结点, nextaddr重新赋值 
	}
	rear->Next = NULL;
	
	return head;
}

List *ListReverse(List *head,int K)
{
	int cnt = 1;
	List *New,*Old,*Tmp;
	New = head->Next;
	Old = New->Next;
	while(cnt < K){
		Tmp = Old->Next;
		Old->Next = New;
		Old->NextAddr = New->Addr;  // 更改NextAddr值,指向逆转后其下一个节点的Addr
		New = Old;
		Old = Tmp;
		cnt++;
	}
	//使反转后的最后一个节点指向下一段子链表的第一个节点
    head->Next->Next = Old;  
    if (Old != NULL){
        //修改Next值,使它指向下一个节点的位置
        head->Next->NextAddr = Old->Addr; 
    }else{
        //如果old为NULL,即没有下一个子链表,那么反转后的最后一个节点即是真个链表的最后一个节点
        head->Next->NextAddr = -1;       
    }
    
	return New;
}

void PrintList(List *L)
{
	List *p = L->Next;  //链表带头结点,因此链表第一个非零项为下一个 
	while( p ){
		if( p->NextAddr != -1){
            //格式输出,%05d意味着如果一个整数不足5位,输出时前面补0 如:22,输出:00022
			printf("%05d %d %05d\n",p->Addr,p->Data,p->NextAddr); 
		}else{
		    //-1不需要以%05d格式输出
			printf("%05d %d %d\n",p->Addr,p->Data,p->NextAddr); 
		}
		p = p->Next;
	}
}

 

 不愿放弃的我只好去找度娘了。【。。。】

借鉴了一位仁兄的代码,终于把代码撸出来了。。。

#include<stdio.h>
#include<stdlib.h>

#define MaxSize 100005

typedef struct Node List;
typedef struct Node{
	int Addr;
	int Data;
	int NextAddr;
	List *Next; 
}LNode;

List *ListReverse(List *head,int K);
void PrintList(List *L);

int main()
{
	int N,K,FirstAddr;
	int num = 0;          //链表建好之后的链表节点数
    int data[MaxSize];   //存data值 节点位置作为索引值
    int next[MaxSize];   //存next值 节点位置为索引
    int tmp;              //临时变量,输入的时候用
    
    scanf("%d %d %d", &FirstAddr, &N, &K);    
    LNode a[N+1];                //能存N+1个结点的数组
    a[0].NextAddr = FirstAddr;   //a[0] 作为头结点
    //读输入的节点   
    for (int i = 1; i < N+1; i++){
        scanf("%d", &tmp);
        scanf("%d %d", &data[tmp], &next[tmp]);        
    }
    
    //构建单链表
    int i = 1;
    while (1){
        if (a[i-1].NextAddr == -1){
            a[i-1].Next = NULL;
            num = i-1;
            break;            
        }
        //a[0].NextAddr = FirstAddr;  //a[0] 作为头结点
        a[i].Addr = a[i-1].NextAddr;
        a[i].Data = data[a[i].Addr]; 
        a[i].NextAddr = next[a[i].Addr];
        a[i-1].Next = a+i;
                
        i++;
    }

	List *p = a;                                  //p指向链表头结点
    List *rp = NULL;                              //反转链表函数的返回值
	if(K == 1){
		PrintList(a);
	}else if(K <= num){
		for(int i=0; i<(num/K); i++){
			rp = ListReverse(p,K);
			p->Next = rp;            // 使p->Next 指向反转后链表的第一个节点
            p->NextAddr = rp->Addr;  // 更改NextAddr值,指向逆转后链表的下一个节点的Addr
            
			int j = 0;
			/*使p指向下一段需要反转的子链表的头结点(第一个节点的前一个节点)*/
            while (j < K){
                p = p->Next;
                j++;
            }
		}
    	PrintList(a);   //输出反转后链表
	}
	return 0;
 } 

List *ListReverse(List *head,int K)
{
	int cnt = 1;
	List *New,*Old,*Tmp;
	New = head->Next;
	Old = New->Next;
	while(cnt < K){
		Tmp = Old->Next;
		Old->Next = New;
		Old->NextAddr = New->Addr;  // 更改NextAddr值,指向逆转后其下一个节点的Addr
		New = Old;
		Old = Tmp;
		cnt++;
	}
	//使反转后的最后一个节点指向下一段子链表的第一个节点
    head->Next->Next = Old;  
    if (Old != NULL){
        //修改Next值,使它指向下一个节点的位置
        head->Next->NextAddr = Old->Addr; 
    }else{
        //如果old为NULL,即没有下一个子链表,那么反转后的最后一个节点即是真个链表的最后一个节点
        head->Next->NextAddr = -1;       
    }
    
	return New;
}

void PrintList(List *L)
{
	List *p = L->Next;  //链表带头结点,因此链表第一个非零项为下一个 
	while( p ){
		if( p->NextAddr != -1){
            //格式输出,%05d意味着如果一个整数不足5位,输出时前面补0 如:22,输出:00022
			printf("%05d %d %05d\n",p->Addr,p->Data,p->NextAddr); 
		}else{
		    //-1不需要以%05d格式输出
			printf("%05d %d %d\n",p->Addr,p->Data,p->NextAddr); 
		}
		p = p->Next;
	}
}

非常感谢这位兄弟-https://blog.youkuaiyun.com/weixin_42182906/article/details/80343251,想法真心不错。

陈越、何钦铭-数据结构作业6:Reversing Linked List链表翻转 Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must output 4→3→2→1→5→6. 立即下载

相关推荐

科研党必备:5个高效查找论文开源代码的宝藏网站(附实战技巧)

本文为科研工作者推荐5个高效查找论文开源代码的宝藏网站,包括ResearchCode和PapersWithCode等工具,提供实战技巧如进阶搜索语法和代码质量评估体系,帮助快速定位高质量实现并提升复现效率。特别分享隐藏代码库的检索方法和智能工作流配置,助力科研党高效完成论文复现。

weixin_28829629的博客 401

PTA 02-线性结构3 Reversing Linked List

02-线性结构3 Reversing Linked List 原题连接 解题参考代码 Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must out

viVi_beea的博客 346

零基础微信理财通基金实操指南(2026版)

先活下来:保证不亏大钱,再想赚钱慢慢变富:年化10%,7年翻倍,这已经很好了开始比完美重要:今天先存100元到零钱通,就是最好的开始现在打开微信,进入理财通,完成第一步风险测评吧!有任何问题可以随时问,我会用最通俗的方式解释。

老韩的Linux云计算架构师进阶之路 1688

02-线性结构3 Reversing Linked List(PTA)

02-线性结构3 Reversing Linked List(25 point(s))Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3...

ckxkobe的博客 1026

01 底盘制动产品之轮速传感器

轮速传感器是用来测量汽车车轮转速的传感器。对于现代汽车而言,轮速信息是必不可少的,汽车动态控制系统(VDC)、汽车电子稳定程序(ESP)、防抱死制动系统(ABS)、自动变速器的控制系统等都需要轮速信息。

汽车软件工程师 | 好奇博主 | 处女座 4656

第170篇 因子图优化入门——图模型在状态估计中的应用

本文系统介绍了从滤波方法到因子图优化的思维转变,重点解析了因子图的原理与应用。首先对比了递推式滤波(卡尔曼滤波等)与全局优化的区别,指出优化方法能利用所有信息且适合处理稀疏结构。然后详细阐述了因子图的二部图结构(变量节点和因子节点)及其将复杂概率分布分解为小因子乘积的优势。文章讲解了因子图优化如何转化为最小二乘问题,并介绍高斯-牛顿法和LM方法等求解技术。最后对比了g2o、Ceres和GTSAM三大主流框架的特点及适用场景,强调了初始化和鲁棒核函数的重要性,为SLAM相关面试提供了实用建议。

郑伟强dev的专栏 374

02-线性结构3 Reversing Linked List (25 分)

02-线性结构3 Reversing Linked List (25 分) Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, the...

小牛的博客 2640

Buck电源设计常见的一些问题(五)MOS管振荡抑制方法(三)

关于开关节点振荡的产生机制和抑制问题,总结了三种抑制方法。第一种方法是利用输入端的高频去耦电容;第二种方法是设计 RC 缓冲电路;第三种方法是优化自举电路的串联电阻。以上三种方法均通过实验加以验证,对开关节点振荡具有很好的抑制作用。应该综合同步 Buck 变换器的各项指标权衡设计。

小幽余生不加糖 1633

数据结构——(PTA)02-线性结构3 Reversing Linked List

02-线性结构3 Reversing Linked List——链表分段逆转

qq_42106610的博客 1408

ubuntu 安装node和npm

在ubuntu中经常需要用到node ,npm,因为npm基本会和node同时安装,所以只需要安装node即可。可以使用 nvm(Node Version Manager)来管理你的 Node.js 版本。

风吹落叶的博客 2291

串口、COM、UART、TTL、USB、RS-232、RS-485、I2C、SPI、CAN、1-WIRE

一文搞定,赶紧收藏吧

记得诚 3万+

7-2 Reversing Linked List (25 分)

7-2 Reversing Linked List (25 分) 这道题可能看题目一头雾水,但是仔细思考就会发现并不太难。 下面是我的思路: 1、用类似静态链表的方法,来表示List,即一个数组存Data,一个数组存Next 2、循环遍历链表,用a[]记录下标。 3、根据K来转置链表,修改Next,后转置a[](顺序很重要)。 #include <stdio.h> #include &...

weixin_44720323的博客 1109

北斗导航 | 卡尔曼滤波与因子图优化对比(附图优化matlab代码)

- 历史数据利用:因子图通过滑动窗口保留历史状态,优化时联合多帧数据;卡尔曼滤波仅依赖当前和上一帧状态 。 - 迭代优化:因子图通过多次迭代逼近最优解,卡尔曼滤波仅单次更新 。 - 误差抑制:因子图的多次线性化可有效抑制非线性误差(如GNSS伪距观测的非线性) 。

尘世冰封的专栏 1303

7-2 Reversing Linked List

7-2 Reversing Linked List 这道题走了许多弯路,究其根本是因为我没有真正从栈的特征或者说是定义入手,没有形成运用数据结构的思维方式。这导致我一开始把问题简单化(简单地比较最大元素后的数目和排序),然后将问题复杂化(对于每一个即将入栈的元素都要和当前出栈元素比较大小)。实际上这道题解题思路很简单,分两步: 1、题目已经给出入栈序列,那就模拟入栈。每次入栈判断栈是否溢出。 2...

weixin_43303540的博客 415

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

[PTA] 7-2 Reversing Linked List 简单方法

题目详情 题目分析: 陈越老师说有些同学投机取巧,所以一些结点被放在了链表外面,防止同学们有排序作弊 但我还是想办法绕过了老师的坑,我知道这门课学的是数据结构,这题本意想让我们练习一下链表,我这样不用链表的方法让大家见笑了 注意并不是所有的节点都在链表里 因为是数据结构,所以本题避免用STL来完成题目 #include <iomanip> #include <iostream> using namespace std; int start, tmp1, tmp2, tmpNum,

yym68686 467

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

PAT 7-2 Reversing Linked List

Given a constantKand a singly linked listL, you are supposed to reverse the links of everyKelements onL. For example, givenLbeing 1→2→3→4→5→6, ifK=3, then you must output 3→2→1→6→5→4; ifK=4, you must output 4→3→2→1→5→6. Input Specification: Eac...

qq466546731的博客 608

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

7-2 Reversing Linked List(30 分)

Given a constant K and a singly linked list L, you are supposed to reverse the links of every Kelements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, ...

qq_41478705的博客 1673

7-2-Reversing Linked List-编程题

7-2-Reversing Linked List-编程题解题代码测试结果问题整理 解题代码 #include<stdio.h> #include<stdlib.h> #define MaxSize 1000000 typedef struct Node node; struct Node { int data; int next; }a[MaxSize]; int m...

Aruasg的博客 295

7-2 Reversing Linked List (25分)(python)

7-2Reversing Linked List(25分) Given a constantKand a singly linked listL, you are supposed to reverse the links of everyKelements onL. For example, givenLbeing 1→2→3→4→5→6, ifK=3, then you...

ZZULI_星.夜 2109

7-2 Reversing Linked List C++代码

题目出自 PTA 7-2 Reversing Linked List Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then ...

mooganic的博客 257

7-2 Reversing Linked List (25分)*

把链表按地址排序,再反转k个节点 const int N = 100005; struct st { int add, data, next; }a[N], *b[N]; int main() { int head, n, k; cin >> head >> n >> k; for(int i = 0; i < n; i ++) { int address, data, next; cin >> address >> da

此地无银三百两 256
上一篇: 7-4 是否同一棵二叉搜索树
下一篇: 最小堆实现哈夫曼树的构造及哈夫曼编码、解码
弱爆了的雪饼
博客等级 码龄10年 76粉丝 70原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值