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;
        }
    }
}

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

相关推荐
进击的女IT2 分钟前
SpringBoot上传图片实现本地存储以及实现直接上传阿里云OSS
java·spring boot·后端
吱吱鼠叔2 分钟前
MATLAB计算与建模常见函数:5.曲线拟合
算法·机器学习·matlab
Miqiuha9 分钟前
lock_guard和unique_lock学习总结
java·数据库·学习
嵌入式AI的盲1 小时前
数组指针和指针数组
数据结构·算法
一 乐1 小时前
学籍管理平台|在线学籍管理平台系统|基于Springboot+VUE的在线学籍管理平台系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习
数云界2 小时前
如何在 DAX 中计算多个周期的移动平均线
java·服务器·前端
阑梦清川2 小时前
Java继承、final/protected说明、super/this辨析
java·开发语言
快乐就好ya3 小时前
Java多线程
java·开发语言
IT学长编程3 小时前
计算机毕业设计 二手图书交易系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·二手图书交易系统
CS_GaoMing4 小时前
Centos7 JDK 多版本管理与 Maven 构建问题和注意!
java·开发语言·maven·centos7·java多版本