LeetCode——Reverse Linked List

         反转链表,用了一个比较笨的方法。

       

public class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null)
            return head;
         ArrayList<Integer> list = new ArrayList<Integer>();
        ListNode p = head;
        while(p!=null) {
            list.add(p.val);
            p = p.next;
        }
        ListNode q = head;
        for(int i=list.size()-1; i>=0; i--) {
            q.val = list.get(i);
            q = q.next;
        }
        return head;
        
    }
}
原文地址:https://www.cnblogs.com/wxisme/p/4518554.html