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.

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

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

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

java 复制代码
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String startPoint = sc.next();
        int pointNum = sc.nextInt();
        int k = sc.nextInt();
        ArrayList<list> list = new ArrayList<>();
        for (int i = 0; i < pointNum; i++) {
            list.add(new list(sc.next(),sc.nextInt(),sc.next()));
        }
       list.sort(new Comparator<Main.list>() {
           @Override
           public int compare(Main.list o1, Main.list o2) {
               return o1.getData() - o2.getData();
           }
       });
        if (pointNum % k == 0 && k != 1) {

            Collections.reverse(list.subList(0,k));
            Collections.reverse(list.subList(k,pointNum));
            for (int i = 1; i < list.size(); i++) {
                list.get(i - 1).setNextAddress(list.get(i).getAddress());
            }
                list.get(pointNum - 1).setNextAddress("-1");
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i).toString());
            }
        } else {
            Collections.reverse(list.subList(0,k));
            for (int i = 1; i < list.size(); i++) {
                list.get(i - 1).setNextAddress(list.get(i).getAddress());
            }
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i).toString());
            }
        }

    }

    static class list {
        String address;
        int data;
        String nextAddress;

        public list(String address, int data, String nextAddress) {
            this.address = address;
            this.data = data;
            this.nextAddress = nextAddress;
        }

        public String getAddress() {
            return address;
        }

        public int getData() {
            return data;
        }

        public String getNextAddress() {
            return nextAddress;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public void setData(int data) {
            this.data = data;
        }

        public void setNextAddress(String nextAddress) {
            this.nextAddress = nextAddress;
        }

        @Override
        public String toString() {
            return
                    address + ' ' + data + ' ' + nextAddress;
        }
    }
}

求助怎么解决这两个问题 ,感谢!!!

相关推荐
cpp_25011 分钟前
P10376 [GESP202403 六级] 游戏
c++·算法·动态规划·题解·洛谷·gesp六级
智者知已应修善业3 分钟前
【51单片机4个IO实现16按键可扩展独立按键64矩阵驱动显示矩阵原值】2023-5-8
c++·经验分享·笔记·算法·51单片机
hui-梦苑3 分钟前
[GROMACS]模拟数据分析前轨迹文件生成-轨迹预处理
人工智能·算法·数据分析
aP8PfmxS24 分钟前
Lab3-page tables && MIT6.1810操作系统工程【持续更新】
java·linux·jvm
无籽西瓜a6 分钟前
【西瓜带你学设计模式 | 第十二期 - 装饰器模式】装饰器模式 —— 动态叠加功能实现、优缺点与适用场景
java·后端·设计模式·软件工程·装饰器模式
蒸汽求职6 分钟前
低延迟系统优化:针对金融 IT 与高频交易,如何从 CPU 缓存行(Cache Line)对齐展现硬核工程底蕴?
sql·算法·缓存·面试·职场和发展·金融·架构
吴声子夜歌6 分钟前
Node.js——zlib压缩模块
java·spring·node.js
海参崴-7 分钟前
深入剖析C语言结构体存储规则:内存对齐原理与实战详解
java·c语言·开发语言
南山乐只8 分钟前
Java并发工具:synchronized演进,从JDK 1.6 锁升级到 JDK 24 重构
java·开发语言·后端·职场和发展
田梓燊8 分钟前
leetcode 239
数据结构·算法·leetcode