1074 Reversing Linked List (25 point(s))

1074. 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 must output 3→2→1 阅读详情

1074 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, 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

链表。

思路很简单,主要考虑两点:

1. 可能存在不在链表中的无效结点;

2. 注意输出格式,比如补零。

一定要读题仔细,看着样例分析的过程中,以为要先排序(样例正好是这样的),然后再每K个元素翻转,结果浪费了整整半个小时。 

#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int MAX = 1e5+7;
int data[MAX];
int nextAdd[MAX];
int main(void){
	int root,N,K;cin>>root>>N>>K;
	int address,key,next;
	for(int i=0;i<N;i++){
		cin>>address>>key>>next;
		data[address]=key;
		nextAdd[address]=next;
	}
	vector<int> v;
	while(root!=-1){
		v.push_back(root);
		root = nextAdd[root];
	}
	for(int i=0;i+K<=v.size();i=i+K){
		reverse(v.begin()+i,v.begin()+i+K);
	}
	for(int i=0;i<v.size();i++){
		if(i==v.size()-1) printf("%05d %d -1\n",v[i],data[v[i]]);
		else printf("%05d %d %05d\n",v[i],data[v[i]],v[i+1]);
	}
	return 0;
} 

 

陈越、何钦铭-数据结构作业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. 立即下载

相关推荐

Setup&Hold互卡问题和Useful Skew的影响

setup和hold互卡问题分析

godping的博客 5097

链表——LinkedList类的概述和实现

上面我们讲了各种链表,每个链表都包括一个LinkedList对象和许多Node对象,LinkedList对象通常包含头和尾节点的引用,分别指向链表的第一个节点和最后一个节点。而每个节点对象通常包含数据部分data,以及对上一个节点的引用prev和下一个节点的引用next,只有下一个节点的引用称为单向链表,两个都有的称为双向链表。next值为null则说明是链表的结尾,如果想找到某个节点,我们必须从第一个节点开始遍历,不断通过next找到下一个节点,直到找到所需要的。栈和队列都是ADT。

weixin_62458944的博客 1598

以ATT&CK为例构建网络安全知识图

随着攻击工具、方法的逐渐升级和复杂化,安全数据的大规模融合,攻防对抗愈加激烈。安全团队如何在浩瀚数据中有效发现高级威胁的蛛丝马迹,如何把网络安全专家的经验、知识有效转化为可复制可扩展的数据分析能力,如何将对抗高级威胁的“炼金术”逐步升级为科学淘金指南,如何将安全从业者从繁重的体力劳动中解放出来,愈发成为安全能力亟需突破的难点和重点,也是我们正在探索的方向。正如医疗行业中治疗疑难杂症依赖医疗专家,网络空间高级威胁的防护和处置也依赖安全专家的经验与知识。

2401_88752684的博客 1092

LinkedList

LinkedList的使用和ArrayList的使用几乎一样。但是它多了一些方法,例如:addFirst,addLast等等。 LinkedList是采用双向循环链表实现的。利用LinkedList实现栈、队列、双向队列。 代码 package com.collection;import java.util.LinkedList;public class LinkedLi...

dengyue5705的博客 162

归因分析计算因子贡献度常见的方法

在归因分析中,我们一般都需要计算出每个因子的贡献度是多少,比如产品DAU上升,对年龄段维度进行拆解,看是不同年龄段的用户对DAU上升的贡献度是多少,一般根据指标的类型,计算贡献度的方法也不一样,下面就列出一些常见的归因分析贡献度的计算方法。

Hubans的博客 4万+

从零到一:基于Rocky Linux 9的Kubernetes高可用集群部署实战

本文提供了一份基于Rocky Linux 9操作系统,从零开始部署Kubernetes高可用集群的完整实战指南。详细讲解了包括系统初始化、容器运行时配置、使用kubeadm工具进行集群初始化、节点加入以及Calico网络插件安装在内的核心步骤,并针对国内网络环境提供了镜像加速等优化方案,旨在帮助读者构建一个稳定可靠的生产级K8s环境。

weixin_29197835的博客 140

1074 Reversing Linked List (25 point(s))

题解 注意[i, i+ K)左闭右开。 C++ &lt; algorithm &gt; 中定义的reverse函数用于反转在[first,last)范围内的顺序 template &lt;class BidirectionalIterator&gt; void reverse (BidirectionalIterator first,BidirectionalIterator last...

徐伯莱 374

CST-Python(Pycharm如何与CST联合仿真,获取S参数)

我们想使用Python与CST联合仿真,其实是不需要自己下载Python的,因为CST在下载的时候就自带了Python3.6,我们只需要找到并将软件的环境设置到相应位置,即可联合仿真。(我的保存路径为D:\Program Files (x86)\CST Studio Suite 2020,在寻找时可以将前面切换成自己的保存路径,后面不需要修改)在CST帮助文档(打开CST软件,按F11即可直接访问帮助文档)里面,进行如下操作,便可以看到CST适配的Python版本为Python3.6!

weixin_52696285的博客 5651

02-线性结构3 Reversing Linked List(25 point(s)) 【链表】

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...

Dup4'Blog 1147

腾讯面试——机器学习岗面试总结

先写合并两个有序数组,接着写合并n个有序的数组,假设总共m个数,讲了一个思路两两合并直到只剩下一个数组,分析一下时间复杂度(MlogN),面试官不满意,然后想了一下说了另一个思路,用n个指针开始指向各个数组的首端,用一个优先队列维护他们的最小值,每次拿出最小值的指针然后插入该指针的下一个值。感觉跟二面面试官聊得还是挺开心的,他详细地介绍了他们部门所做的业务,然后问了一下什么时候能来实现,我说只能暑假七八两个月,他觉得有点晚,这样子不利于转正,有些人都实习半年的,回去讨论一下,正式批之前给我答复。

FPGA/MATLAB学习教程/源码/项目合作开发 1232

【AI智能体】飞书 AI 智能客服机器人搭建与使用实战操作详解

飞书 AI 智能客服机器人搭建与使用实战操作详解

congge 6059

PATA 1074.Reversing Linked List (25)解题报告

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 must ...

热心市民小黎的博客 557

AI + 药物临床试验研发赛道

研究日期:2026-07-09 | 研究主题:AI + 药物临床试验研发赛道 | 聚焦岗位:Agent 平台架构师 / AI 工程架构师。

AI天才研究院 146

1074 Reversing Linked List (25point(s)) - C语言 PAT 甲级

1. 利用数组存储节点 2. 利用初始地址及 next 地址,为列表排序,并以 -1 为终点,记录真实节点数目 3. 根据 k 值,反转链表,利用数组下标,对称交换即可 * 最初想用链表的 next 地址值尾插反转链表,突然想到,此地址值并不是真的内存地址(卒~)

有三只土豆 324

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Advanced Level 1074 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, then you must output 3→2→1→6→5→4; if K=4, you must output 4→3→2→1→5→6. Input Specification:: E

蒟蒻 321

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

PAT----A1074 Reversing Linked List (25point(s))

A1074 Reversing Linked List (25point(s)) 题意 链表每k个元素反转。 思路 reverse()。 总结 最后总有一个测试点过不去,然后发现又被坑了,原来输入的节点有可能根本没有接上以head作为头部的链表,所以最终的链表长度可能并不是n。 Sample Input: 00100 6 4 00000 4 99999 00100 1 12309 68237 6 ...

qq_43578870的博客 215

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

PAT_甲级_1074 Reversing Linked List (25point(s)) (C++)【链表反转reverse】

目录 1,题目描述 题目大意 2,思路 3,AC代码 1,题目描述 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 ...

&再见萤火虫&的博客 240

1074 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 must output 3→2→1→6→5→4; if K ...

ysq96的博客 328

1074 Reversing 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 must output 3→2→1→6→5→4; ifK=4, ...

zbchenchanghao的博客 156

PTA(A)1074 Reversing Linked List (25point(s))(坑)

思路: 最后一个点是坑,可能存在孤立点。 代码: #include <iostream> #include <algorithm> #include <vector> #include <cstdio> using namespace std; struct node { int address, next; int data; } mp[100005]; vector<node>a; int main() { in.

Sqssq的博客 754

1025 反转链表 (25 分)&&1074 reversing linked list

题目 给定一个常数 K 以及一个单链表 L,请编写程序将 L 中每 K 个结点反转。例如:给定 L 为 1→2→3→4→5→6,K 为 3,则输出应该为 3→2→1→6→5→4;如果 K 为 4,则输出应该为 4→3→2→1→5→6,即最后不到 K 个元素不反转。 输入格式: 每个输入包含 1 个测试用例。每个测试用例第 1 行给出第 1 个结点的地址、结点总个数正整数 N (≤10^​5​​ )...

m714星云的博客 329

PAT1074 Reversing Linked List (25)详细题解

02-1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a constant K and a singly linked list L, you are supposed ...

weixin_30750335的博客 81

1074. Reversing Linked List (25)-PAT甲级真题

1074. 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, t

柳婼 の blog 3903

02-线性结构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 must output 3→2→1→6→5→4; if K

进阶的Kaiser@ZJU !!! 1500
上一篇: 1052 Linked List Sorting (25 point(s))
下一篇: 1099 Build A Binary Search Tree (30 point(s))
coderwait
博客等级 码龄9年 20粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值