剑指Offer 24. 反转链表

  也就是LeetCode25里用到了反转子链表的方法,只不过这次直接反转整个链表。头插法就能解决了

  

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode t = new ListNode(-1);
        t.next = null;
        ListNode a = head;
        ListNode b = null;
        while(a!=null){
            b = a.next;
            a.next = t.next;
            t.next = a;
            a = b;
        }
        return t.next;
    }
}
原文地址:https://www.cnblogs.com/dloooooo/p/13755716.html