【PAT甲级真题】- Reversing Linked List (25)

题目来源

Reversing Linked List (25)

题目描述点击链接自行查看

注意点:

  • 是反转每 K 个元素,不是反转前 K 个
  • 地址补齐到 5 位输出

题目描述

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.

输入描述:

Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N

(<= 105) 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.

输出描述:

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.

输入例子:

复制代码
00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

输出例子:

复制代码
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1

思路简介

静态链表模拟题

也是做过很多类似的题目了

把静态链表读到数组里面然后用 reverse 反转就行了

非常建议大家把静态链表存到数组里面去操作

直接模拟链表操作虽然说很规范而且工程肯定也会这么写

但是这是OI,怎么简单怎么写,能空间换时间就最好

因为我们只有三个小时,没时间写那么规范的模板了

就算写出来调 bug 指不定还要调好久

遇到的问题

  1. 只反转了前 K 个 wa 了一次

代码

cpp 复制代码
/**
 * https://www.nowcoder.com/pat/5/problem/4033
 * 模拟
 */
#include<bits/stdc++.h>
using namespace std;

const int N=1e6+10;
struct Node{
    int curr,v,next;
    
    Node():curr(0),v(0),next(0){}
    Node(int curr,int v,int next):curr(curr),v(v),next(next){}
}node[N];

void solve(){
    int head,n,k;
    cin>>head>>n>>k;
    for(int i=0;i<n;++i){
        int cu,v,ne;
        cin>>cu>>v>>ne;
        node[cu]={cu,v,ne};
    }
    vector<Node>res;
    int curr=head;
    while(curr!=-1){
        res.emplace_back(node[curr]);
        curr=node[curr].next;
    }
    int len=res.size();
    for(int i=0;i<len&&i+k<=len;i+=k)
        reverse(res.begin()+i,res.begin()+i+k);
    
    for(int i=0;i<len;++i){
        cout<<setfill('0')<<setw(5)<<res[i].curr<<' '<<res[i].v<<' ';
        if(i==len-1){
            cout<<"-1";
        }
        else {
            cout<<setfill('0')<<setw(5)<<res[i+1].curr<<'\n';
        }
    }
}

int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //fstream in("in.txt",ios::in);cin.rdbuf(in.rdbuf());
    int T=1;
    //cin>>T;
    while(T--){
        solve();
    }
    return 0;
}
相关推荐
运筹vivo@16 分钟前
LeetCode 2405. 子字符串的最优划分
c++·算法·leetcode·职场和发展·哈希表
数智工坊17 分钟前
视觉-语言-动作模型解剖学:从模块、里程碑到核心挑战
论文阅读·人工智能·深度学习·算法·transformer
yuannl1028 分钟前
数据结构----二叉排序树(ai修改版)
数据结构
有点。44 分钟前
C++(枚举法一练习题)
开发语言·c++·算法
basketball6161 小时前
C++ 单例模式完全指南:从饿汉式到现代 C++ 的最佳实践
java·c++·单例模式
黎阳之光1 小时前
视听融合新范式!黎阳之光打破视觉边界,声影协同赋能全域智慧管控
大数据·人工智能·物联网·算法·数字孪生
iiiiyu1 小时前
集合进阶(Map集合)
java·大数据·开发语言·数据结构·编程语言
小江的记录本1 小时前
【Java基础】核心关键字:final、static、volatile、synchronized、transient(附《思维导图》+《面试高频考点清单》)
java·前端·数据结构·后端·ai·面试·ai编程
玖釉-1 小时前
栈——栈的定义及基本操作
c++·windows·算法·图形渲染
不想写代码的星星1 小时前
C++ 内存序六件套:从完全同步到爱咋咋地
c++