--------------------------------------------------------------------------------------------------------------------------------------------------------
时间限制:1秒 空间限制:32768K 代码长度限制 100 KB
--------------------------------------------------------------------------------------------------------------------------------------------------------
题目描述
给定一个常数K以及一个单链表L,请编写程序将L中每K个结点反转。例如:给定L为1→2→3→4→5→6,K为3, 则输出应该为3→2→1→6→5→4;如果K为4,则输出应该为4→3→2→1→5→6,即最后不到K个元素不反转。
输入描述:
每个输入包含1个测试用例。每个测试用例第1行给出第1个结点的地址、结点总个数正整数N(<=105)、以及 正整数K(<=N),即要求反转的子链结点的个数。结点的地址是5位非负整数,NULL地址用-1表示。 接下来有N行,每行格式为: Address Data Next 其中Address是结点地址,Data是该结点保存的整数数据,Next是下一结点的地址。
输出描述:
对每个测试用例,顺序输出反转后的链表,其上每个结点占一行,格式与输入相同。
输入例子:
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
--------------------------------------------------------------------------------------------------------------------------------------------------------
实现思路:
(1).创建表示节点的对象Node,将输入的节点用map存储,key为节点地址,value为节点;
(2).创建一个list来存储节点集合,通过第一个节点地址,在map中找到对应的节点node,将节点加入list中;
(3).根据node的next地址,从map中找到对应的节点,并加入到list中,循环该过程,直到节点next不合法为止;
(4).通过list长度和K,确定反转链表需要执行的次数leapTime,循环处理list,根据K及起始节点位置,将各个区段的节点顺序倒序处理;
(5).由于节点位置变换,节点在list中对应的next地址不是下一个节点的address,循环遍历list,逐一处理节点的next地址,指向下一个节点的address;
(6).逐一打印list中节点元素信息即可。
--------------------------------------------------------------------------------------------------------------------------------------------------------
package com.biyao.algorithm.niuke.a1;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Main_a1_015 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
String first = scan.next();
int n = scan.nextInt();
int k = scan.nextInt();
Map<String,Node> nodeMap = new HashMap<String,Node>();
for (int i = 0; i < n; i++) {
String address = scan.next();
int value = scan.nextInt();
String next = scan.next();
nodeMap.put(address, new Node(address,value,next));
}
List<Node> nodeList = new ArrayList<Node>();
nodeList.add(0, nodeMap.get(first));
for (int i = 0; i < Integer.MAX_VALUE; i++) {
if(nodeMap.containsKey(nodeList.get(i).next)){
nodeList.add(i+1, nodeMap.get(nodeList.get(i).next));
}else{
break;
}
}
int len = nodeList.size();
int leapTime = len/k;
int start = 0,end = Math.min(start + k, len);
for (int i = 0; i < leapTime; i++) {
int left = start,right = end-1;
for (int j = start; j < end; j++) {
if(left >= right){
break;
}
Node node = nodeList.get(left);
nodeList.set(left, nodeList.get(right));
nodeList.set(right, node);
left++;
right--;
}
start = start + k;
end = Math.min(start + k, len);
}
for (int i = 0; i < len-1; i++) {
if(nodeList.get(i) == null){
break;
}
if(nodeList.get(i+1) != null){
nodeList.get(i).next = nodeList.get(i+1).address;
}
}
for (int i = 0; i < nodeList.size(); i++) {
System.out.println(nodeList.get(i).address + " " + nodeList.get(i).value + " " + nodeList.get(i).next);
}
}
}
private static String formatNum(int num){
if(num >= 0 && num <= 9){
return "0000" + num;
}else if(num >= 10 && num <= 99){
return "000" + num;
}else if(num >= 100 && num <= 999){
return "00" + num;
}else if(num >= 1000 && num <= 9999){
return "0" + num;
}
return num + "";
}
}
class Node{
String address;
int value;
String next;
public Node(String address,int value,String next){
this.address = address;
this.value = value;
this.next = next;
}
}
但该程序性能有问题,提交总是超时,暂时没找到可通过的方式。