PAT:Reversing Linked List

     这道题用Java写,确实觉得蛋疼,数据点5通不过说是运行超时,在网上搜寻了一下应该是数据量太大了,采用排序方法必死,只有暂时不管了。。。

 

先上题目:

--------------------------------------------------------------------------------------------------------

Given a constant K and a singly linked list L, you are supposedto 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 firstline contains the address of the first node, a positive N (<=105) which is the total number of nodes, and a positiveK (<=N) which is the length of the sublist to be reversed. Theaddress of a node is a 5-digit nonnegative integer, and NULL isrepresented 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 isan integer, and Next is the position of the next node.

Output Specification:

For each case, output the resulting ordered linked list. Eachnode occupies a line, and is printed in the same format as in theinput.

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.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;

import sun.tools.jar.resources.jar;

public class Main {
 public static void main(String[] args) {
  // TODO Auto-generated methodstub
  List mLinkStructList = newArrayList();
  
  @SuppressWarnings("resource")
  Scanner sc = newScanner(System.in);
  long firstAddress =sc.nextLong();
  int N = sc.nextInt();
  int K = sc.nextInt();
  
  //读取数据
  for(int i = 0; i
   MLinkStructmLinkStruct = new MLinkStruct();
   mLinkStruct.thisAddress= sc.nextInt();
   mLinkStruct.value= sc.nextInt();
   mLinkStruct.nextAddress= sc.nextInt();
   mLinkStructList.add(mLinkStruct);
  }
  
  //排序
  long next = firstAddress;
  boolean isEnd = false;
  for (int i = 0; i < N; i++){
   if(isEnd){//发现最后一个元素,其余就删除掉
    mLinkStructList.remove(mLinkStructList.size()-1);
   }else{
    for(int j = i; j < N; j++) {
     if(mLinkStructList.get(j).thisAddress== next){
      if(i==j) break;//发现当前元素处于正确的位置,就什么都不做
      ChangeStruct(mLinkStructList,i, j);
      if(mLinkStructList.get(i).nextAddress == -1) {
       isEnd= true;//已经是最后一个数据了
       //N= i+1;//存在非链中数据,所以真实的N改变了
      }
      break;
     }
    }
    next=mLinkStructList.get(i).nextAddress;  
    
  }
  
  //MyPrint(mLinkStructList);
  
  
  //创建一个空栈
  Stack mStack = newStack();
  N =mLinkStructList.size();
  int restN = N;//剩下的未处理数据
  int j = K;
  
  for (int i = 0; i < N/K;i++) {
   j = K;
   while(j>0){//入栈
    mStack.push(mLinkStructList.get(0));   
    mLinkStructList.remove(0);
    if(--restN == 0) break;//已经没有未处理的数据
    j--;
   }
   while(!mStack.isEmpty()){
    mLinkStructList.add(mStack.pop());
   }
  }
  
  while(restN-->0){
   mLinkStructList.add(mLinkStructList.get(0));
   mLinkStructList.remove(0);
  }
   
  
  
  
  ChangeNextAddress(mLinkStructList);
  MyPrint(mLinkStructList); 
 }
 

 
 public static void ChangeStruct(List list, intpositon1, int position2){
  MLinkStruct temp =list.get(position2);
  list.set(position2,list.get(positon1));
  list.set(positon1, temp);
 }
 
 public static void MyPrint(Listls){ 
  for(int i = 0; i <ls.size(); i ++){ 
   System.out.print((newDecimalFormat("00000").format(ls.get(i).thisAddress)) + " ");
   System.out.print((ls.get(i).value)+ " ");
   if(i ==ls.size()-1)System.out.print(ls.get(i).nextAddress);
   elseSystem.out.print(newDecimalFormat("00000").format(ls.get(i).nextAddress));
   System.out.print("\n"); 
  
 

 public static void ChangeNextAddress(Listls){
  MLinkStruct temp = null;
  for (int i = 0; i <ls.size()-1; i++) {
   temp =ls.get(i);
   temp.nextAddress= ls.get(i+1).thisAddress;
   ls.set(i,temp);
  }
  ls.get(ls.size()-1).nextAddress= -1;//将最后一个元素的nextaddress设为-1
 }
 
 static class MLinkStruct{
  longthisAddress;  
  long nextAddress;
  int value;
  public MLinkStruct() {
   // TODOAuto-generated constructor stub
   thisAddress =0;
   nextAddress =0;
   value =0;
  }
 
}

-----------------------------------------------------------------------------------------------

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值