题目描述

输入一个链表,反转链表后,输出链表的所有元素。

解题思路

用一个临时指针存储原本应该的下一位

代码实现

/** * */
package 链表;

/** * <p> * Title:ReverseList * </p> * <p> * Description: * </p> * * @author 田茂林 * @data 2017年8月22日 下午3:02:54 */
public class ReverseList {
    public ListNode NodeReverseList(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode p = head;
        ListNode pre = null;
        ListNode pNext = null;
        while (p.next != null) {
            pNext = p.next;    //用来存储原来下一位的位置
            p.next = pre;
            pre=p;
            p=pNext;
        }
        p.next = pre;
        return p;

    }
}