一个序列顺序为{0,1,2.......N}
节点定义如下?public void Node(){
String value;
Node next;
}
怎样让他的顺序倒置过来;
{n,n-1.......0}
那位高手指点一下本人对数据结构不是很熟啊?

解决方案 »

  1.   

    上班临时写的,测试通过,没有考虑效率
    public class C {
    public static void main(String []args) {
    int l = args.length ;
    int [] a = new int[l] ;
    int temp ;
    for(int i=0;i<l;i++) {
    a[i] = Integer.parseInt(args[i]) ;
    }
    for(int i=0;i<=(a.length/2 -1);i++) {
    temp = a[i] ;
    a[i] = a[l-i-1] ;
    a[l-i-1] = temp ;
    }

    for(int i=0;i<l;i++) {
    System.out.println(a[i]);
    }
    }
    }
      

  2.   

    C:\>javac C.javaC:\>java C 1 2 3 4
    输入的顺序是:
     1 2 3 4
    倒序后的结果是:
     4 3 2 1
      

  3.   


        public static Node g(Node head) {
            Node previous = null;
            Node current = head;
            while (current != null) {
                Node next = current.next;
                current.next = previous;
                previous = current;
                current = next;
            }
            return previous;
        }
      

  4.   


    public class Test{  /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
    List<Integer> l1=new ArrayList<Integer>();
    l1.add(1);
    l1.add(2);
    l1.add(3);
    l1.add(4);
    l1.add(5);
    l1.add(6);
    l1.add(7);
    l1.add(8);
    l1.add(9);
    l1.add(10);
    Collections.reverse(l1);
    Node pre=null;
    Node newNode=null;
    Node nodeHead=null;
    for(int i=0;i<l1.size();i++){
    if(pre!=null){
    newNode=new Node();
    newNode.setValue(l1.get(i));
    pre.setNext(newNode);
    pre=newNode;
    }else{
    pre=new Node();
    pre.setValue(l1.get(i));
    nodeHead=pre;
    }

    }
    pre=nodeHead;
    while(pre!=null){
    System.out.println(pre.getValue());
    pre=pre.getNext();
    }

    }
    class Node{
    private int value;
    private Node next;
    public Node(int arg0,Node next){
    this.value=arg0;
    this.next=next;
    }
    public Node(){

    }
    public Node getNext() {
    return next;
    }

    public void setNext(Node next) {
    this.next = next;
    }
    public int getValue() {
    return value;
    }
    public void setValue(int value) {
    this.value = value;
    }
    }