7-2 Reversing Linked List(25 分)
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
思路:
1、用java中的LinkedList类建立链表,先按照顺序串起来。
2、翻转顺序搞清楚,先用temp节点copy第k个节点,然后从后到前依次翻转。
3、这题目有个坑,就最后一个测试点会给你额外的链表外的节点,要把这些节点剔除出去。
4、java跟c写同样的程序,c出结果只要2ms,java要100+ms,所以倒数第二个测试点,当数据量最大时,c需要100+ms,题目允许最大运行时间是400ms,不用说,java运行超时了。
java:
测试点 | 结果 | 耗时 | 内存 |
---|---|---|---|
0 | 答案正确 | 137 ms | 23520KB |
1 | 答案正确 | 133 ms | 22584KB |
2 | 答案正确 | 136 ms | 23696KB |
3 | 答案正确 | 133 ms | 23404KB |
4 | 答案正确 | 129 ms | 23296KB |
5 | 运行超时 | 0 ms | 0KB |
6 | 答案正确 | 138 ms | 23680KB |
import java.util.Scanner;
// 翻转链表
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
Node head = new Node();
head.naddr = in.nextInt();
int n = in.nextInt();
int k = in.nextInt();
Node [] a = new Node[n+1];
a[0]=head;
for(int i = 1;i<=n;i++) {
Node nd = new Node();
nd.addr = in.nextInt();
nd.value = in.nextInt();
nd.naddr = in.nextInt();
a[i] = nd;
}
int j = 0;
for(int count = 0 ; count<n; count++) {
for(int i = 0;i < n+1; i++ ) {
if(a[i].addr == a[j].naddr)
{
a[j].next = a[i];
j=i;
break;
}
}
}
Node js = head;
n = 0;
while(js.naddr!=-1)
{
n++;
js = js.next;
}
// print(head,n);
Node result = reverse(head,n,k);
print(result,n);
}
private static void print(Node result,int n) {
// TODO Auto-generated method stub
result = result.next;
for(int i= 0 ; i<n;i++) {
System.out.printf("%05d", result.addr);
System.out.print(" "+result.value+" ");
if(result.naddr!=-1) {
System.out.printf("%05d", result.naddr);
System.out.println();
}
else
System.out.print(-1);
result = result.next;
}
}
private static Node reverse(Node head, int n, int k) {
// TODO Auto-generated method stub
int count = n/k;
int rest = n%k;
Node point = head;
for( int i = 0; i<count ; i++) {
Node temp = new Node();
Node temp2 = get(point,k);
temp.naddr = get(point,k).naddr;
temp.next = get(point,k).next;
for(int j = k ; j>=2 ; j--) {
get(point,j).naddr = get(point,j-1).addr;
get(point,j).next = get(point,j-1);
// System.out.println(get(point,j).naddr);
}
get(point,1).naddr = temp.naddr;
get(point,1).next = temp.next;
point.naddr = temp2.addr;
point.next = temp2;
point = get(point,k);
}
if(rest == 0 )
point.naddr = -1;
return head;
}
private static Node get(Node point, int k) {
// TODO Auto-generated method stub
for(int i =0; i<k; i++)
point = point.next;
return point;
}
}
class Node{
public int addr = -1;
public int value = -1;
public int naddr = -1;
public Node next = null;
Node(){
;
}
}