做这题出了点问题,答案不是完全正确的,测试点5运行超时,测试点6错误,以后再来做一遍。
题目
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.Scanner;
public class lineStruct3 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int firstAdd = s.nextInt();
int size = s.nextInt();
int reverseSize = s.nextInt();
linkedList list = readLine(s, size);
linkedList sortL = sortList(list, size, firstAdd);
int x = size/reverseSize;
linkedList rl = new linkedList();
linkedList l = new linkedList();
// linkedList l = reverse(sortL, reverseSize);
for(int i =0;i<x;i++) {
l = reverse(sortL, reverseSize);
rl.addList(l);
}
rl.addList(sortL);
sorted(rl);
rl.printQ();
}
public static void sorted(linkedList l) {
Node n = l.getHead();
while(n.next != null) {
n.setNextAdd(n.next.getAdd());
n = n.next;
}
}
public static linkedList reverse(linkedList list, int renum) {
linkedList l1 = new linkedList();
for(int i = 0; i<renum; i++) {
Node n = list.delNode();
l1.addNode(n);
}
Node pre = l1.getHead();
Node cur = pre.next;
pre.next = null;
pre.setNextAdd(-1);
while(cur != null) {
Node temp = cur.next;
cur.next = pre;
cur.setNextAdd(pre.getAdd());
pre = cur;
cur = temp;
}
l1.setHead(pre);
return l1;
}
public static linkedList sortList(linkedList list, int size, int firstAdd) { //对输入链表进行排序
linkedList sortL = new linkedList();
Node temp = findNode(list, firstAdd);
Node element = new Node(temp.getAdd(),temp.getData(),temp.getNextAdd());
int a = temp.getNextAdd();
sortL.addNode(element);
while(--size != 0) {
temp = findNode(list, a);
Node n = new Node(temp.getAdd(),temp.getData(),temp.getNextAdd());
sortL.addNode(n);
a = temp.getNextAdd();
}
return sortL;
}
public static Node findNode(linkedList list, int add) { //根据地址寻找节点
Node temp = list.getHead();
while(temp != null) {
if(temp.getAdd() == add)
break;
else
temp = temp.next;
}
return temp;
}
public static linkedList readLine(Scanner s, int size) {
linkedList list = new linkedList();
while(size-- != 0) {
Node node = new Node(s.nextInt(), s.nextInt(), s.nextInt());
list.addNode(node);
}
return list;
}
}
class Node{
private int add; //地址
private int Data; //存储的数
private int nextAdd; //下一个地址
public Node next; //下一个节点
public Node() {
this.add = 0;
this.Data = 0;;
this.nextAdd = 0;
this.next = null;
}
public Node(int add, int Data, int nextAdd) {
this.add = add;
this.Data = Data;
this.nextAdd = nextAdd;
this.next = null;
}
public int getAdd() {
return this.add;
}
public int getData() {
return this.Data;
}
public int getNextAdd() {
return this.nextAdd;
}
public void setAdd(int i) {
this.add = i;
}
public void setData(int i) {
this.Data = i;
}
public void setNextAdd(int ne) {
this.nextAdd = ne;
}
public void printQ() {
System.out.println("删除" + this.Data);
}
}
class linkedList{
private Node head;
private Node last;
public linkedList() {
head = null;
last = head;
}
public boolean isEmpty() {
if(head == null)
return true;
else
return false;
}
public void addNode(Node node) {
if(node != null) {
if(isEmpty()) {
head = node;
last = node;
}
else {
last.next = node;
node.next = null;
last = node;
}
}
}
public void addList(linkedList list) {
if(isEmpty()) {
this.head = list.getHead();
this.last = list.getLast();
}
else {
this.last.next = list.getHead();
this.last = list.getLast();
}
}
public Node delNode() {
if(head == last && head != null) { //首位项重合且不为空
Node n = new Node(head.getAdd(),head.getData(),head.getNextAdd());
head = null;
last = head;
return n;
}
else {
Node n = new Node(head.getAdd(),head.getData(),head.getNextAdd());
head = head.next;
return n;
}
}
public void setHead(Node head) {
this.head = head;
Node n = this.head;
while(n.next != null) {
n = n.next;
}
this.last = n;
}
public Node getLast() {
return this.last;
}
public Node getHead() {
return this.head;
}
public void printQ() {
Node temp = head;
if(isEmpty())
System.out.print("链表为空");
else {
while(temp.next != null) {
System.out.printf("%05d %d %05d",temp.getAdd(),temp.getData(),temp.getNextAdd());
System.out.println();
temp = temp.next;
}
System.out.printf("%05d %d -1",temp.getAdd(),temp.getData());
}
}
}