1.  /** 
  2.      * 遍历,将当前节点的下一个节点缓存后更改当前节点指针 
  3.      */  
  4.     public static Node reverse2(Node head) {  
  5.         if (head == null)  
  6.             return head;  
  7.         Node pre = head;// 上一结点  
  8.         Node cur = head.getNext();// 当前结点  
  9.         Node tmp;// 临时结点,用于保存当前结点的指针域(即下一结点)  
  10.         while (cur != null) {// 当前结点为null,说明位于尾结点  
  11.             tmp = cur.getNext();  
  12.             cur.setNext(pre);// 反转指针域的指向  
  13.   
  14.             // 指针往下移动  
  15.             pre = cur;  
  16.             cur = tmp;  
  17.         }  
  18.         // 最后将原链表的头节点的指针域置为null,还回新链表的头结点,即原链表的尾结点  
  19.         head.setNext(null);  
  20.           
  21.         return pre;  
  22.     }  
  23. }  
posted on 2017-10-10 11:07  blythe  阅读(182)  评论(0编辑  收藏  举报