zoukankan      html  css  js  c++  java
  • java反转链表

    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. }  
  • 相关阅读:
    Codeforces Round #420 (Div. 2) A-E
    Codeforces Round #419 (Div. 2) A-E
    Bzoj4423 [AMPPZ2013]Bytehattan
    51nod1471 小S的兴趣
    Bzoj2629 binomial
    51nod1056 最长等差数列 V2
    51nod1055 最长等差数列
    51nod1110 距离之和最小 V3
    20. 有效的括号
    155.最小栈
  • 原文地址:https://www.cnblogs.com/blythe/p/7644570.html
Copyright © 2011-2022 走看看