1074 Reversing Linked List /1025 反转链表 java

90 篇文章 0 订阅
12 篇文章 0 订阅

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

一定要去牛客网上的pat提交,如果你是用java写的代码。重要的事情说一遍

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int startAddress = scanner.nextInt(), n = scanner.nextInt(), k = scanner.nextInt();
        Node1025[] linkedNodes = new Node1025[100000 + 10];
        for (int i = 0; i < n; ++i) {
            Node1025 eachNode = new Node1025();
            eachNode.address = scanner.nextInt();
            eachNode.data = scanner.nextInt();
            eachNode.next = scanner.nextInt();
            linkedNodes[eachNode.address] = eachNode;
        }
        scanner.close();
        int p = startAddress, order = 0;
        while (p != -1) {
            linkedNodes[p].order = order++;
            p = linkedNodes[p].next;
        }
        Arrays.sort(linkedNodes, new MyComparator1025());
        n = order;
        int count = n / k, leftIndex = 0, rightIndex = k - 1;
        for (int i = 1; i <= count; ++i) {

            int tempRight = rightIndex;
            while (rightIndex > leftIndex) {
                System.out.printf("%05d %d %05d\n", linkedNodes[rightIndex].address,
                        linkedNodes[rightIndex].data,
                        linkedNodes[rightIndex - 1].address);
                --rightIndex;
            }
            System.out.printf("%05d %d ", linkedNodes[leftIndex].address,
                    linkedNodes[leftIndex].data);
            int lastNext = -1;
            rightIndex = (i + 1) * k - 1;
            if (rightIndex + 1 <= n) {
                lastNext = linkedNodes[rightIndex].address;
            } else if (n % k != 0) {
                lastNext = linkedNodes[tempRight].next;
            }

            if (lastNext == -1) {
                System.out.println("-1");
            } else {
                System.out.printf("%05d\n", lastNext);
            }
            leftIndex = i * k;

        }
        if (n % k != 0) {
            // 还有剩余的
            for (count = count * k; count < n; ++count) {
                System.out.printf("%05d %d ", linkedNodes[count].address,
                        linkedNodes[count].data);
                if (count == n - 1) {
                    System.out.println("-1");
                } else {
                    System.out.printf("%05d\n", linkedNodes[count].next);
                }

            }
        }


    }
}

class Node1025 {
    int address;
    int data;
    int next;
    Integer order = 100000 + 10;

    @Override
    public String toString() {
        return "Node1025{" +
                "address=" + address +
                ", data=" + data +
                ", next=" + next +
                ", order=" + order +
                '}';
    }
}

class MyComparator1025 implements Comparator<Node1025> {

    @Override
    public int compare(Node1025 o1, Node1025 o2) {

        if (o1 != null && o2 != null) {
            return o1.order.compareTo(o2.order);
        } else if (o1 == null && o2 != null) {
            return 1;
        } else if (o1 != null && o2 == null) {
            return -1;
        } else {
            return 0;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值