一、题目
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
二、解答
import java.util.*;
public class Main {
static int index = -1;
static ArrayList<String> stack = new ArrayList<>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] strs = sc.nextLine().split(" ");
String headAddr = strs[0];
int dataNum = Integer.parseInt(strs[1]);
int revNum = Integer.parseInt(strs[2]);
/*
Save Input Data
define 2 maps:addrMap, dataMap
addrMap: key=currentAddress value=nextAddress
dataMap: key=currentAddress value=data
*/
HashMap<String,String> addrMap = new HashMap<>();
HashMap<String,String> dataMap = new HashMap<>();
//Format : Address Data Next
for (int i = 0; i < dataNum; i++) {
strs = sc.nextLine().split(" ");
addrMap.put(strs[0],strs[2]);
dataMap.put(strs[0],strs[1]);
}
//Get Address List
ArrayList<String> addrList = new ArrayList<>();
getAddrList(headAddr, addrList, addrMap);
//避免多余无关的结点(不在链表上的)对结果造成的影响
dataNum = addrList.size();
process(dataNum,revNum,addrList,dataMap);
}
public static void getAddrList(String address, ArrayList<String> list, HashMap<String,String> map){
if (!address.equals("-1")){
list.add(address);
getAddrList(map.get(address), list, map);
}
}
public static void push(String address){
stack.add(address);
index++;
}
public static String[] pop(){
String nextAddr = stack.get(index-1);
String currAddr = stack.remove(index--);
return new String[]{currAddr, nextAddr};
}
public static void reverseAndPrint(int count, int revNum, ArrayList<String> addrList, HashMap<String,String> dataMap){
/*
push在压入的过程中会先压入下一组颠倒数的首个地址 count + 2 * revNum 所以循环时要保证至少还剩下 2 * revNum个结点
如果dataNum % revNum == 0 正好全部进行颠倒,则可循环进行颠倒的次数为 dataNum / revNum - 1,剩下revNum单独处理
如果dataNum % revNum != 0 不能进行全颠倒,则可循环进行颠倒的次数为 dataNum / revNum - 1,剩下revNum + dataNum % revNum
*/
push(addrList.get(count + 2 * revNum - 1));
for (int i = 0; i < revNum; i++, count++) push(addrList.get(count));
for (int i = 0; i < revNum; i++) {
String[] addrs = pop();
//Output Format: currAddr data nextAddr
System.out.println(addrs[0] + " " + dataMap.get(addrs[0]) + " " + addrs[1]);
}
stack.remove(index--);
}
public static void process(int dataNum, int revNum, ArrayList<String> addrList, HashMap<String,String> dataMap){
int times = dataNum / revNum - 1;
int count = times * revNum;
for (int i = 0; i < times; i++) {reverseAndPrint(revNum * i, revNum, addrList, dataMap);}
//dataNum % revNum == 0正好全部进行颠倒,则可循环进行颠倒的次数times为 dataNum / revNum - 1,剩下revNum单独处理
//dataNum % revNum > 0不能进行全颠倒,则可循环进行颠倒的次数times为 dataNum / revNum - 1,剩下revNum + dataNum % revNum
if (dataNum % revNum == 0){
//处理剩下的revNum个结点
for (int i = 0; i < revNum; i++, count++) push(addrList.get(count));
//注意最后一个结点无下一个结点地址,只能弹出revNum - 1次
for (int i = 0; i < revNum - 1; i++) {
String[] addrs = pop();
System.out.println(addrs[0] + " " + dataMap.get(addrs[0]) + " " + addrs[1]);
}
//弹后最后一个结点
String lastAddr = addrList.get(dataNum - revNum);
System.out.println(lastAddr + " " + dataMap.get(lastAddr) + " -1");
} else {
//先处理revNum个剩下的结点
push(addrList.get(count + revNum));
for (int i = 0; i < revNum; i++, count++) push(addrList.get(count));
for (int i = 0; i < revNum; i++) {
String[] addrs = pop();
System.out.println(addrs[0] + " " + dataMap.get(addrs[0]) + " " + addrs[1]);
}
//再处理dataNum % revNum个剩下的结点,注意最后一个结点无下一个结点地址,输出-1
for (int i = 0; i < dataNum % revNum - 1; i++, count++) {
System.out.println(addrList.get(count) + " " + dataMap.get(addrList.get(count)) + " " + addrList.get(count+1));
}
//弹出最后一个结点
System.out.println(addrList.get(count) + " " + dataMap.get(addrList.get(count)) + " -1");
}
}
}
思路:
- 考虑如何存放输入数据:创建两个map【map1:key=当前地址,value=下一个地址】【map2:key=当前地址,value=对应值】,【注意】地址要存为字符串类型,因为整型00000→0
- 考虑如何遍历这些数据:对map1理一理,按照一个个键值对遍历一下,就可以得到一个有序地址的list
- 考虑如何进行倒置:将数据分为多个大小为revNum的组,每revNum个数进行堆栈处理一次,【注意】每两次堆栈处理前后需要关联起来,需要完成一个地址的连接,数据末尾需要慎用堆栈处理,进行分情况讨论。
思考:
地址要存为字符串类型,因为整型00000→0,可以借鉴C中的 printf("%05d",....)而定义为整型,输出补0即可,不知道java中是否可以?