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. 立即下载

相关推荐

睿尔曼超轻量仿人机械臂-ROS第一篇 启动机械臂的Moveit

本文主要讲述了睿尔曼六轴机械臂在ROS中的功能包配置,及如何根据文件启动Moveit做仿真。

realman_Rop的博客 7490

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

QT触摸屏开发:如何通过重写事件处理实现精准按键长按检测

本文针对QT触摸屏开发中按键长按检测不精准的常见问题,深入剖析了传统信号机制失效的原因,并提供了通过重写事件处理函数(event)的解决方案。文章详细介绍了创建自定义TouchButton类、直接处理QTouchEvent以实现可靠长按检测的完整步骤与代码示例,并分享了进阶优化与避坑指南,帮助开发者实现跨平台一致的精准交互。

weixin_29207149的博客 95

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

Windows Server安装Intel I219-V网卡驱动完整教程

在Windows Server系统上安装Intel I219-V网卡驱动的方法。

小白成长历程 5172

基于STM32开发的智能语音控制系统

本文将介绍如何使用STM32微控制器开发一个智能语音控制系统,通过语音识别模块、OLED显示屏、Wi-Fi模块等实现对家用电器或其他设备的语音控制。智能语音控制系统的核心是STM32微控制器,通过语音识别模块识别用户的语音指令,并根据指令控制家用电器或其他设备。:定期测试语音识别模块、继电器控制模块、OLED显示屏和Wi-Fi模块的工作状态,确保系统能够在各种情况下正常运行,并保持智能语音控制系统的可靠性。本系统同样适用于办公环境,可以通过语音指令控制办公室设备,如照明、空调等,提供智能化的办公体验。

stm32d1219的博客 6911

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

LC-3 机器语言 指令集

生活本就沉闷,但跑起来就有风

笔记 1万+

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

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

qq_42106610的博客 1408

U盘无法识别?一条命令轻松修复,数据不丢失

问题描述 U盘经常会遇到提示:无法识别或者需要重新格式等问题,例如下图 修复方法 WIN+R输入cmd 输入chkdsk+盘符:/F /F是修复的意思 更多内容 U盘无法识别或提示需要格式化?别急着格式化丢失数据!本文教你使用Windows自带的命令快速修复U盘文件系统错误,简单几步就能解决问题,让你的U盘恢复正常使用💾✨

XiaoqiangClub的博客 640

SAPGUI 与 ADT 的时代分野:在多云架构下,谁还能代表现代 SAP 开发

摘要(149字) SAP开发工具正经历代际更迭:SAPGUI基于专有协议(NI/DIAG)的厚客户端架构难以适配多云环境,而ADT(ABAP Development Tools)通过HTTP(S)协议和Eclipse生态成为ABAP Cloud/RAP开发的唯一入口。关键差异体现在:ADT支持CDS、行为定义等云原生对象建模,天然对齐BTP的零信任网络;集成ATC、Git等DevOps工具链;而SAPGUI无法创建RAP核心对象,在跨平台、自动化方面存在硬限制。官方文档明确将ADT作为Clean Core战

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 390

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

蓝牙模块HM-10手册

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

7-2 Reversing Linked List

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

weixin_43303540的博客 415

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

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

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

yym68686 467

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

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

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、付费专栏及课程。

余额充值