首页 > 试题广场 >

Reversing Linked List (25)

[编程题]Reversing Linked List (25)
  • 热度指数:2850 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
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.

输入描述:
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.


输出描述:
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.
示例1

输入

00100 6 4<br/>00000 4 99999<br/>00100 1 12309<br/>68237 6 -1<br/>33218 3 00000<br/>99999 5 68237<br/>12309 2 33218

输出

00000 4 33218<br/>33218 3 12309<br/>12309 2 00100<br/>00100 1 99999<br/>99999 5 68237<br/>68237 6 -1
public class Main {
 	    public static final int MAX=100000;
    public Node[] list;
    public int headAddress,rhead;
    public int N,K;
    public class Node{
        int data=0;
        int next=0;
        int address=0;
        public Node(int address,int data,int next){
            this.address=address;
            this.data=data;
            this.next=next;
        }
    }


    public void createPoly(){
        int nodeAddress,i=0;
        int data,next;
        Scanner s=new Scanner(System.in);
        headAddress=s.nextInt();
        N=s.nextInt();
        K=s.nextInt();

        if(headAddress<0||headAddress>99999||N>100000||N<0||K<0)
        {
            System.out.println("Input Error");
            return;
        }
        //variable can be declared but not initilize,
        //here we initialize the list!
        list=new Node[MAX];
        while(i<N){
            //Actually,it is not needed to use getLine.
           String str=s.nextLine();
            String[] arr=str.split(" ");
            int[] arrx=new int[arr.length];
            for(int k=0;k<arr.length;k++){
                 arrx[k]=Integer.parseInt(arr[k]);
            }
            nodeAddress=s.nextInt();
            data=s.nextInt();
            next=s.nextInt();
            list[nodeAddress]=new Node(nodeAddress,data,next);

            i++;
        }
    }

    public void printList(Node[] xlist,int xheadAddress){
        while(xlist[xheadAddress].next!=-1){
            System.out.printf("%05d %d %05d\n",xheadAddress,xlist[xheadAddress].data,xlist[xheadAddress].next);
            xheadAddress=xlist[xheadAddress].next;
        }
        System.out.printf("%05d %d %05d\n",xheadAddress,xlist[xheadAddress].data,xlist[xheadAddress].next);

    }

    public Node[] reverse(){
        //list
        Stack<Node> s1=new Stack<Node>();
        Node[] rlist=new Node[MAX];
        int rheadAddress=0,rear=0,Last=0;
        if(N<K)
            return null;
        int count=N/K;

        while(count>0){
            for(int i=0;i<K;i++){
                s1.push(list[headAddress]);
                rheadAddress=headAddress;
                headAddress=list[headAddress].next;
            }
            if(count==N/K)
            {
                rhead=s1.peek().address;
            }
            count--;
            for(int j=0;j<K;j++){
                //if N%K==0 , it will pop
                if(!s1.empty()){
                    //这里花费了一晚上,对于内存管理的不了解!
                    Node xtmp=s1.pop();
                    Node tmp=new Node(xtmp.address,xtmp.data,xtmp.next);
                    rlist[rheadAddress]=tmp;
                    if(count==0&&j==0){
                        Last=tmp.address;
                    }
                    //using top()!!!!!!!
                    if(s1.empty()){
                        if(count==0)
                            rear=tmp.address;
                        break;
                    }
                    rlist[rheadAddress].next=s1.peek().address;
                    rheadAddress=rlist[rheadAddress].next;
                }
            }
        }
        System.out.printf("%d %d\n",Last,rear);
        if(N%K!=0){
            int num=N%K+1;//Start from the node "1", the node rear.
            //So including rear there shall add 1.
            while(num>0){
                Node tmp=new Node(rear,list[rear].data,list[Last].next);
                rlist[rear]=tmp;
                //rlist[rear].next=list[Last].next;
                Last=list[Last].next;
                rear=Last;
                num--;
            }

        }
        return rlist;

    }

    public static void main(String[] args) {
     	Main r=new Main();
        Scanner s=new Scanner(System.in);
      /*  int[] arrx=new int[4];
        String str=s.nextLine();
        String[] arr=str.split(" ");
        for(int i=0;i<arr.length;i++){
            arrx[i]=Integer.parseInt(arr[i]);
        }
        System.out.println(arrx[0]);
        System.out.println(arrx[1]);
*/
        Node[] rlist;
        r.createPoly();
        r.printList(r.list,r.headAddress);
        rlist=r.reverse();

        System.out.println(rlist[r.rhead].data);
        int rhead=r.rhead;
       r.printList(rlist,rhead);
    }
   
}

编辑于 2016-09-07 17:56:27 回复(0)

问题信息

难度:
1条回答 15886浏览

热门推荐

通过挑战的用户

Reversing Linked List (25)